A web scripting language is called JavaScript. It contains its own data types, much like any other computer language. The type of data that a variable can carry is defined by its data type in a language.
There are eight basic data types in JavaScript.
Data Types | Description | Example |
---|---|---|
String | Represents textual data | let str = 'Hi', let str2 = "Hello", let str3 = `Hello World` |
Number | An integer or a floating-point number | let num = 3, let num2 = 3.234, let num3 = 3e-2 |
BigInt | An integer with arbitrary precision | let num = 900719925124740999n, let num = 1n |
Boolean | Any of two values: true or false | let flag = true |
undefined | A data type whose variable is not initialized | let a; |
null | Denotes a null value | let a = null; |
Symbol | Data type whose instances are unique and immutable | let value = Symbol('hello'); |
Object | key-value pairs of collection of data | let student = { }; |
String
Text is stored in strings. Strings in JavaScript are enclosed in quotes:
- Single quotes: 'Hello'
- Double quotes: "Hello"
- Backticks: `Hello`
Example::
// Strings const firstName = "Code"; const lastName = "Solution"; const result = `Name: ${firstName} ${lastName}`; console.log(result); // Name: Code Solution
Number
Numerals represent floating-point and integer numbers (decimals and exponentials). Additionally, a number type can be +Infinity, -Infinity, or NaN. (not a number).
const number1 = 3; const number2 = 3.433; const number3 = 3e5; // 3 * 10^5 const number4 = 3 / 0; console.log(number4); // Infinity const number5 = -3 / 0; console.log(number5); // -Infinity // strings can't be divided by numbers const number6 = "abc" / 3; console.log(number6); // NaN
BigInt
Only numbers less than and greater than -(2sup>53/sup> -1) can be represented using the Number type in JavaScript. However, you can use the BigInt data type if you require a larger number than that.
By adding n to the end of an integer, a BigInt number is produced.
// BigInt value const num1 = 900719925124740998n; const num2 = 900719925124740998n; const num3 = 10; // Adding two big integers const result1 = num1 + num2; console.log(result1); // "1801439850249481996n" // Error! BitInt and number cannot be added const result2 = num1 + num2 + num3; console.log(result2); // Uncaught TypeError: Cannot mix BigInt and other types
Boolean
This data type represents logical entities. Boolean values can only be either true or false.
const dataChecked = true; const valueCounted = false;
undefined
A value that is not assigned is represented by the undefined data type. A variable's value will be undefined if it is declared but its value is not yet assigned.
let name; console.log(name); // undefined let name = undefined; console.log(name); // undefined
null
Null is a special value in JavaScript that denotes an empty or undefined value.
const number = null;
Symbol
Symbol values can be defined as values with the data type Symbol. Symbol is a singular, immutable primitive value.
// Two symbols with the same description const value1 = Symbol('hello'); const value2 = Symbol('hello'); let result = (value1 === value2) ? true : false; // false; // Note: Though value1 and value2 both contain 'hello', they are different as they are of the Symbol type.
Object
A complicated data type called an object enables us to store data collections.
const employee = { firstName: 'John', lastName: 'K', email: '[email protected]' };