Author: M Abo Bakar Aslam
Data Types
In programming, data types are an important concept. To operate on variables effectively, it is essential to understand their type. Without data types, a computer cannot safely process data.
JavaScript has 8 data types:
- Number
- BigInt
- String
- Boolean
- Undefined
- Null
- Symbol
- Object
1. Number
Numbers can be integers, floating-point values, or any numeric data.
Note: JavaScript integers are only accurate up to 15 digits.
Example Code 1:
In following example, we used let as we discussed in previous lesson that modern approach to declare variable is based on const and let. In this example, we just declared some numeric variables and then displayed their values.
// Numbers:
let length = 16;
let weight = 7.5;
//Exponential Notation
let y = 123e5; // result will be 12300000
let z = 123e-5; // result will be 0.00123
//displaying values of variables
console.log(length)
console.log(weight)
console.log(y)
console.log(z)2. BigInt
All JavaScript numbers are stored in a 64-bit floating-point format (IEEE 754). Because of this limitation, very large integers cannot be represented accurately.
To handle such cases, JavaScript introduced BigInt (ES2020), which allows storing large integer values beyond the safe limit:
- Maximum safe integer: +9007199254740991
- Minimum safe integer: -9007199254740991
BigInt enables precise representation of integers beyond this range.
Example Code 2:
In below example, we discussed two method to declare BigInt type variable. One is by calling BitInt() and second is by appending n at end of the integer.
// BigInt by 1st method
let x = BigInt("123456789012345678901234567890");
// BigInt by second method
let y = 9999999999999999n //appendring n to the end of an integer
//displaying values of variables
console.log(x)
console.log(y)3. String
A string represents a sequence of characters.
Strings can be defined using:
- Double quotation marks (
" ") - Single quotation marks (
' ')
Example Code 3:
In following piece of code, we just declared some string variables and then displayed their values. String data can be declared by using either double or single quotation marks.
// String with double quotation marks
let color = "Yellow";
// Strning with single quotation marks
let lastName = 'Johnson';
//displaying values of variables
console.log(color)
console.log(lastName)4. Boolean
A Boolean data type can have only two values:
truefalse
It is commonly used in conditional statements and logical operations.
Example Code 4:
In following piece of code, we just declared some Boolean variables and then displayed their values.
// Booleans
let x = true;
let y = false;
//displaying values of variables
console.log(x)
console.log(y)5. Undefined
If a variable is declared but not assigned a value, it is automatically assigned the value undefined.
Example Code 5:
Following piece of code doesn't have some error. Output of this code is undefined
let z
//displaying values of variables
console.log("Value of z is: ",z)6. Null
If a variable is intentionally assigned an empty value, it is considered null. Null represents the absence of a value.
Example Code 6:
Following piece of code doesn’t have some error. Output of this code is nothing.
let z = "" //null data with double quotation marks
let x = '' //null data with single quotation marks
//displaying values of variables
console.log("Value of z is: ",z)
console.log("Value of x is: ",x)7. Symbol
A Symbol represents a unique value. It is primarily used to create unique identifiers for object properties.
Example Code 7:
let z = "$" //Symbol data with double quotation marks
let x = '#' //Symbol data with single quotation marks
//displaying values of variables
console.log("Value of z is: ",z)
console.log("Value of x is: ",x)8. Object
Objects are complex data types that store collections of data in key-value pairs.
An object can contain:
- Properties (key-value pairs)
- Arrays
- Other objects
- Dates
Objects are defined using curly braces { } and properties are written as name: value.
Example Code 8:
In following piece of code, the object (person) has 4 properties: firstName, lastName, age, and eyeColor. It is noted that const is used for declaring objects we discussed above. Moreover, curly braces are used for declaration.
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
console.log("person age: ", person.age)
console.log("person first name: ", person.firstName)Example Code 9:
In above example, object is declared in a proper way. The object can also be declared in one line as you can see in below example.
const person = {firstName : "John", lastName : "Doe", age : 50, eyeColor : "blue" };
console.log("person age: ", person.age)
console.log("person first name: ", person.firstName)Example Code 10:
In above two examples, object's properties are accessed by using dot operator “.” but we can also use square braces and double quotation marks for the same purpose. See below example, in which above code is written but square braces and double quotation marks are used to display age and firstName of the object.
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
console.log("person age: ", person["age"])
console.log("person first name: ", person["firstName"])8.a. Array
Arrays are used to store multiple values in a single variable.
- Arrays are written using square brackets
[ ] - Elements are separated by commas
- Each element has an index (starting from 0)
Although arrays are technically objects in JavaScript, they are specifically designed for ordered collections.
Note: Arrays are a special type of object. The typeof operator returns "object" for arrays, but they are best treated as a separate data structure. Arrays use numeric indexes, while objects use named properties.
Example Code 11:
The following code declares (creates) an array called cars, containing three items (car names). Array's elements can be accessed by using index number of elements. The first item has index number is 0, second has 1, and so on. Line 2 to 4 show starting, first and second element of cars array. It is noted that array is declared by using const as we discussed in previous lesson.
const cars = ["Saab", "Volvo", "BMW"]
console.log(cars[0])
console.log(cars[1])
console.log(cars[2])Example Code 12:
In following code, we declared array in multiple lines. This method is only for increasing readability of code. Output is same as your saw for previous code.
const cars = [
"Saab",
"Volvo",
"BMW"
]
console.log(cars[0])
console.log(cars[1])
console.log(cars[2])Example Code 13:
Above codes can also be write in following way. In below mentioned method, we initially declared cars array as blank and then move values in array at its index numbers.
const cars = []
cars[0]= "Saab"
cars[1]= "Volvo"
cars[2]= "BMW"
console.log(cars[0])
console.log(cars[1])
console.log(cars[2])8.b. Date
Date objects are used to work with dates and times. A Date object represents a specific moment in time and is commonly used for:
- Current date and time
- Formatting dates
- Performing date calculations
Example Code 14:
//const is used to store value
const current_date = new Date()
//display the obtained-value by above line
console.log(current_date)