A primitive value or data type is something that is not an object and has no methods associated with it. In other words, primitive data is simple data with no additional methods and properties.
We can use typeof (value) operator to know the data type of any given value.
typeof 1; // number typeof "dev.to;" // string typeof null; // object typeof undefined; // undefined typeof true; // boolean typeof 1n; // bigint let sym = Symbol("key"); typeof key; // symbol
A boolean is a data type that can only take two values, which are, true and false.
A boolean value is commonly used in comparison and conditional operations.
We can use Boolean() constructor to create a boolean object. However, this is generally considered a bad practice and should be avoided.
let x = new Boolean(true); typeof x; // boolean
Don't confuse a boolean primitive type with a Boolean object.
The value null represents the absence of the object.
let boo = null; typeof boo; //null
In the above example, the variable boo exists but it doesn't have any value associated with it.
undefined is a value that is automatically assigned to variables that have been declared
var x; typeof x; // undefined
A number data type contains numerical values. You can define positive, decimal (floating point), and negative values. It also has a positive and negative Infinite value.
We can use Number() constructor to initialize an object of number datatype.
var num = 123; typeof num; // number // using constructor var boo = new Number(123) typeof boo; // object typeof boo.valueOf(); // number
A string is a data type that consists of a character or sequence of characters in single quotes('example') or double quotes("example") or in back-tick quotes.
We can use the String() constructor function to build an object of type string.
let string1 = "Double quotes"; let string2 = 'Single quotes'; typeof string1; // string let string4 = new String("string constuctor"); typeof string4; // object typeof string4.valueOf(); // stirng
A symbol is a datatype that provides an anonymous, unique value that can be used as an object property.
Symbols are introduced in ES6.
A symbol doesn't have a constructor so we cannot create a symbol object using the new keyword.
let sym1 = new Symbol('a'); // TypeError let sym2 = Symbol('a'); // symbol is created let sym3 = Symbol('a'); // symbol is created sym2 === sym3; // false
The last sentence is false because both sym2 and sym3 are unique keys.
refer this page for more details about symbols.
bigint primitive datatype is introduced in ES10.
The maximum value of bigint is 2^51 -1.
A bigint contains n as a suffix to a number.
let x = 1; let y = 1n; typeof x; // number typeof y; // bigint
Thankyou for reading the article please provide your valuable feedback in the comments.