In Javascript, several data types are supported such as Boolean, Number, String, Object, Null, and Undefined. However, all data types, no matter what they are, are declared with the help of a common keyword called "var". Data types in JavaScript may be of the type primitive or object.
The following are the primitive data types that are supported in JavaScript,
- Boolean = It represents a logical value of an object or a statement, and can assume the value of "true" or "false".
- Null = Null represents the absence of any value to a given object. It indicates that the variable points to no object.
- Undefined = A variable whose value hasn't been assigned yet is called is undefined. A statement returns undefined if the variable which is being talked about in the statement has not been assigned a value. A function returns undefined if no value is returned by the function.
- String = A collection of alphabets or characters is called a string.
- Number = A number in Javascript can be declared also using just the "var" keyword, irrespective of whether the number is an integer, float, negative number, or decimal. This is unlike many other programming languages where we require different keywords for different kinds of numbers.
Let's discuss the Undefined and Null data type.
The difference between Undefined and Null data type,
- A variable that has been declared but not yet assigned a value to it has a data type of “undefined”.
- On the other hand, Null is an assignment value. The null value represents an empty or non-existent reference value.
Here an example,
In the above statement, a variable has an explicit value.
another way of creating a variable is,
Now, the above variable would have a type undefined.
Apart from the primitive, we also have a data type called Object in Javascript. Let's discuss the object.
Object
An object is a collection of properties. You can imagine a JavaScript object to be a kind of a hashmap wherein each property is stored in the form of a key/value pair. These properties may in themselves consist of any kind of data type, primitive, or an object within an object.