Primitive data types are the basic values that we use to build a webpage.
Note: A primitive value is neither a property nor a method. It's a basic value used in a property, method, variable, or function.
JavaScript has seven (7) primitive values:
JavaScript allows the following number types:
Integers (For instance, 300
)
Decimals (For example, 300.75
)
Scientific notations (For instance, 3e2
)
let nbr1 = 300;let nbr2 = 300.5;console.log(nbr1 + nbr2);
We create string data types by enclosing zero or more characters in quotes.
JavaScript allows the following quotes:
Double quotes (For instance, "300"
)
Single quotes (For example, '300'
)
Backtick quotes (For instance, `300`
let strExample = "Hello World";console.log(strExample);let strExample2 = 'Welcome To Educative';console.log(strExample2);let strExample3 = `Learning is fun`;console.log(strExample3);
Boolean data types state the falseness or truthfulness of an expression. true
and false
are the two boolean values in JavaScript.
console.log(Boolean(300)); // trueconsole.log(Boolean(undefined)); // false
Note: JavaScript's values are true except
0
(zero),false
,""
(empty string),NaN
(Not a Number),null
,undefined
, and0n
(BigInt zero).
JavaScript automatically assigns the undefined
primitive data type to any variable declared without an initial value.
let myName;console.log(typeof myName); // undefined
We use the null
primitive data type to indicate an intentional absence of a value.
let myName = null;console.log(typeof myName); // "object"
In the code snippet above, the null
expresses that we intentionally left the variable empty. Therefore, the typeof myName
returned "object"
—not undefined
.
We create symbol data types by invoking the Symbol()
function in a JavaScript runtime environment.
let mySym = Symbol("randomTest");console.log(typeof mySym); // "symbol"
We mainly use the BigInt (Big Integer) data type for arbitrarily lengthy integers.
We can create a BigInt value by:
Appending the letter "n"
to an integer
Providing the integer as the BigInt()
function's argument
console.log(BigInt(375)); // 375n