javascript undefined typeof null boolean

Tut#3 JavaScript Data Types Part 1: Undefined, Null, Boolean and TypeOf

What do we discuss in this article?

  1. The Undefined Type
  2. The Null Type
  3. The Boolean Type
  4. The typeof operator

Data Types in JavaScript

Javascript data types

slidenerd

JavaScript supports 6 data types, undefined, null, Boolean, String, Number and Object. Every other type that is created by you or seems unfamiliar is created from one of these.

A] The Undefined Type

It is a special value given to any variable which has been created without specifying a value.

Here is a snapshot taken from Google Chrome. You’ll see a similar dialog popping up in the other browsers as well.

javascript alert undefined chrome

slidenerd

You can also test if a particular variable has the value undefined by using ‘==’.

Here is a snapshot of what the above alert() will display inside Safari.

javascript undefined test

slidenerd

Never explicitly set the value of a variable to undefined like this var message=undefined. The undefined value is provided only for comparison purposes which means you can use it in your code to check if some variable is undefined. Here below is the difference between undefined and a variable that has not been declared.

 B] The Null Type

When  you talk about your score in an exam, if your score is 0, it means you haven’t scored anything. The same way null is a value that indicates the special value ‘nothing’  for Objects in JavaScript. At this point, if you aren’t aware of Objects, don’t fret. We’ll talk about Objects a lot more in our upcoming posts. Null basically means NO MEANINGFUL VALUE. 

 When you create a variable and if you can’t think of a sensible value to assign, assign it the null value.

What do you think this will yield?

Null and Undefined are considered superficially equal. However, they have very different uses. You always assign a null value to a variable when you cannot think of a meaningful value but you never assign an undefined value to a variable under any circumstance, 

alert undefined equals null

slidenerd

C] The Boolean Type

Just like the switch inside your room which can be either ON or OFF, a Boolean is a special type with only 2 values: true or false. If you are a programmer who knows another language, true is NOT 1 and false is NOT 0 either in JavaScript.

Any value can be converted to Boolean in JavaScript with the help of Boolean() function. If you aren’t aware of functions, don’t fret, we’ll talk about that in the upcoming posts as well.

1) Conversion To Boolean

Let’s first understand the rules of conversion to a Boolean and then work some examples out.

Data Type What becomes true? What becomes false?
Boolean true false
String A non-empty String such as “Vivz” An empty String such as “”
Number Non-zero number including infinity 0, NaN
Object Any Object with non-null value Object with null value

Undefined is always converted to false.

You’ll be using Boolean mostly in conditional statements and loops which again we’ll see in the upcoming posts.

D] The typeof operator

If you have a variable, you can get its type by using the typeof operator.

The Value of the variable What typeof will return?
undefined “undefined”
true or false “boolean”
Text such as “Vivz” or “” or “bad” “string”
Numbers such as 0, 99 -125 “number”
null or any meaningful Object “object”
A function “function”

 CONCLUSION

In the next post, we’ll talk about Numbers and their properties in JavaScript. In the meantime, stay tuned and don’t forget to like and share this post if you found it helpful, Thanks!