Data Types in JavaScript — The Weird Parts
Tags: javascript, webdev, programming, beginners
Published: 2022-02-12 09:19:42

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

```js
const name = 'someone';    // data type is string
const num = 101;           // data type is number
const isAlive = true;      // data type is boolean
```
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:

```js
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
```

## 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:

```js
const obj = { a: "apple", b: "ball" };
console.log(typeof obj);  //object
```

## 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**

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

### 2. undefined

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

```js
let name;
console.log(typeof name);   //undefined
```

### 3. null

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

```js 
let name = null;
console.log(typeof name);   //null
```

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:

```js
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
```

### 2. Weirdness with null

```js
console.log(typeof null);  //object
    
const name = null;
console.log(name);   //null
    
console.log(typeof name);   //object
```
### 3. Weirdness with objects

```js
 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
```

## 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
- [Stackoverflow Forum](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript)
- [Flavio Copes Blog](https://flaviocopes.com/javascript-difference-null-undefined/)
- [App Dividend Article](https://appdividend.com/2019/04/08/javascript-null-undefined-and-nan-tutorial-with-example/)

### More on JavaScript
- [Variables in JavaScript, Scope and Hoisting](https://withinbracket.com/posts/variables-in-javascript-scope-and-hoisting/)
- [Type Conversion in JavaScript - The Magic](https://withinbracket.com/posts/type-conversion-in-javascript-the-magic/)
- [An Introduction to Loops in JavaScript](https://withinbracket.com/posts/loops-in-javascript/)
- [JavaScript Equality - Double Equals vs Triple Equals](https://withinbracket.com/posts/javascript-equality-double-equals-vs-triple-equals/)

🎉Thanks for sticking around. Keep Learning.

> **📢This article is officially published on [Within Bracket](https://withinbracket.com/posts/data-types-in-javascript-the-weird-parts/)**

📌 Find More Articles [Here](https://withinbracket.com/archives)