Primitive and Non-primitive data-types
Primitive data types:
The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
Ex- Number, String, Boolean, Undefined, Null, Symbol, BigInt.
Non-primitive data types:
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.
Ex-Object, Array
JavaScript Datatypes:
String
Number
Bigint
Boolean
Undefined
Null
Symbol
Object
"use strict"; // treat all JS code as newer version.
console.log(3 + 3) // code readability should be high
console.log("Hitesh")
let name = "hitesh"
let age = 18
let isLoggedIn = false
let state;
// number => 2 to power 53
// bigint
// string => ""
// boolean => true/false
// null => standalone value
// undefined =>
// symbol => unique
// object
console.log(typeof undefined); // undefined
console.log(typeof null); // object
Console Output:
6
Hitesh
undefined
object
The important differences between Null and Undefined are:
SN | Null | Undefined |
---|---|---|
1. | It is an assignment value. It can be assigned to a variable which indicates that a variable does not point to any object. | It is not an assignment value. It means a variable has been declared but has not yet been assigned a value. |
2. | It is an object. | It is a type itself. |
3. | The null value is a primitive value which represents the null, empty, or non-existent reference. | The undefined value is a primitive value, which is used when a variable has not been assigned a value. |
4. | Null indicates the absence of a value for a variable. | Undefined indicates the absence of the variable itself. |
5. | Null is converted to zero (0) while performing primitive operations. | Undefined is converted to NaN while performing primitive operations. |