Categories
JavaScript Tutorials

Primitive Data Types

1. Number—this includes floating point numbers as well as integers, for example 1, 100, 14.4

2. String—any number of characters, for example “a”, “one”, “one 2 three”.

3. Boolean—can be either true or false.

4. Undefined—when you try to access a variable that doesn’t exist, you get the special value undefined. The same will happen when you have declared a variable, but not given it a value yet. JavaScript will initialize it behind the scenes, with the value undefined.

5. Null—this is another special data type that can have only one value, the null value. It means no value, an empty value, nothing. The difference with undefined is that if a variable has a value null, it is still defined, it only happens that its value is nothing. You’ll see some examples shortly.

Note: Finding out the value type —the typeof operator. If you want to know the data type of a variable or a value, you can use the special typeof operator. This operator returns a string that represents the data type. The return values of using typeof can be one of the following—”number”, “string”, “boolean”, “undefined”, “object”, or “function”. In the next few sections, you’ll see typeof in action using examples of each of the five primitive data types.