What is a primitive data type?
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.
Types of primitive data in JavaScript
JavaScript has seven (7) primitive values:
1. Number primitive data type
JavaScript allows the following number types:
Integers (For instance,
300)Decimals (For example,
300.75)Scientific notations (For instance,
3e2)
Example
let nbr1 = 300;let nbr2 = 300.5;console.log(nbr1 + nbr2);
2. String primitive data type
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`
Example
let strExample = "Hello World";console.log(strExample);let strExample2 = 'Welcome To Educative';console.log(strExample2);let strExample3 = `Learning is fun`;console.log(strExample3);
3. Boolean primitive data type
Boolean data types state the falseness or truthfulness of an expression. true and false are the two boolean values in JavaScript.
Example
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).
4. Undefined primitive data type
JavaScript automatically assigns the undefined primitive data type to any variable declared without an initial value.
Example
let myName;console.log(typeof myName); // undefined
5. Null primitive data type
We use the null primitive data type to indicate an intentional absence of a value.
Example
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.
6. Symbol primitive data type
We create symbol data types by invoking the Symbol() function in a JavaScript runtime environment.
Example
let mySym = Symbol("randomTest");console.log(typeof mySym); // "symbol"
7. BigInt primitive data type
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 integerProviding the integer as the
BigInt()function's argument
Example
console.log(BigInt(375)); // 375n
Free Resources
- undefined by undefined