DEV Community

Cover image for Data Types in JavaScript — The Weird Parts
Sarvesh Prajapati
Sarvesh Prajapati

Posted on

Data Types in JavaScript — The Weird Parts

If you have been into programming for a while, you know what are data types and why they are important in typically every programming language.

But if you don’t know about data types, well it’s just the type of data (value) you stored in a variable — as simple as that.

Let’s see an example

const name = 'someone';    // data type is string
const num = 101;           // data type is number
const isAlive = true;      // data type is boolean
Enter fullscreen mode Exit fullscreen mode

So let’s deep dive into data types and explore them.

JavaScript has two types of data types: Primitive and Non-Primitive.

Let’s look at Primitive Data Types

Primitive data types are a type which contain only one particular value, it can be a string, number or boolean like the example above

Here is an example:

console.log(typeof 10);     //number
console.log(typeof true);   //boolean
console.log(typeof "sdf");  //string
console.log(typeof 10.5);   //number
console.log(typeof false);  //boolean
Enter fullscreen mode Exit fullscreen mode

Now, let’s look at Non-Primitive Data Types

Non-Primitive data types is a type which contains a collection of data and this data can be of multiple types: primitive or non-primitive

In JavaScript, the object is the most important non-primitive data-type. As we all know objects are the barebones of JavaScript, so we’ll deal with them in another article.

For now, let’s check them out for the sake of understanding the non-primitive data types.

Let’s see an example of this:

const obj = { a: "apple", b: "ball" };
console.log(typeof obj);  //object
Enter fullscreen mode Exit fullscreen mode

Some more data types

Along with primitive and non-primitive data types, JavaScript has three more data types.

1. function()

In any programming language, as well as in JavaScript, the thing we use most often is the function.

The function has its own data type called function

const whoAmI = (who) => {
    console.log(`I am ${who}`);   // I am No one
};
whoAmI('No one');
console.log(typeof whoAmI);   // function
Enter fullscreen mode Exit fullscreen mode

2. undefined

It simply represents that the value is not assigned to a variable.

let name;
console.log(typeof name);   //undefined
Enter fullscreen mode Exit fullscreen mode

3. null

The data type null represents that there is no value — nothing— empty.

let name = null;
console.log(typeof name);   //null
Enter fullscreen mode Exit fullscreen mode

You might be confused between undefined and null. But there is one simple explanation

The undefined is implicit, meaning we don’t have to set a value (or error value), JavaScript automatically gets it. Whereas in the case of null, it is explicit, meaning we have to set it on our own like in the example above.

Okay! But what about the weird part?

In the context of data types, I might say that JavaScript is weird in some aspects. So far, I have seen some weirdness with JavaScript like :

1. Weirdness with in-built constructor functions

In JavaScript, we have some in-built construction functions to define the data types of a variable (which you should not use) like String, Object, Date, etc.

Look at the code below:

console.log(typeof String);  //function

const place = String("somewhere");
console.log(typeof place);   //string

const fruit = new String('fruit');
console.log(typeof fruit);   //object

console.log(typeof Date);   //function

const now = new Date();
console.log(typeof now);     //object

const date = Date;
console.log(typeof date);    //function
Enter fullscreen mode Exit fullscreen mode

2. Weirdness with null

console.log(typeof null);  //object

const name = null;
console.log(name);   //null

console.log(typeof name);   //object
Enter fullscreen mode Exit fullscreen mode

3. Weirdness with objects

 console.log(typeof Object);   //function

const item = ['a', 'd'];
console.log(typeof item);   //object

const obj = { a: "apple", b: "ball" };
console.log(typeof obj);   //object
Enter fullscreen mode Exit fullscreen mode

Conclusion

So, that’s all about Javascript data types and their weirdness. There are also some use-cases that I haven’t mentioned yet. So, if you want to know about them, write the code and explore on your own.

Finally, JavaScript is weird, but that’s the reason I love JavaScript. In future content, we will explore more about JavaScript and its weirdness.

References

More on JavaScript

🎉Thanks for sticking around. Keep Learning.

📢This article is officially published on Within Bracket

📌 Find More Articles Here

Top comments (0)