Javascript Data Types (String, Number, Boolean, Array, Object)
In this, we will go through all the data types of javascript in detail with examples.
What are data types?
- Data types in general define what specific kind of data we want to store in and get manipulated within the program.
So mainly the categories of DataTypes in javascript are:
- Primitive data type
- Composite data type (Non-Primitve)
So let’s explore these categories of data types one by one:
1.Primitive data type
These data types can hold only a single value. For example
- String
- Number
- Boolean
a. String data type
- To represent textual data we use String data type.
- Let’s say we want to create a sequence of characters So this is how we create them:
let name = “Siddhant”;
So you see above we used a variable let with variable name as name, our String in double quotes “ ” or you can use single quotation ‘ ‘ marks also and last but not least separating them with Assignment operator equal = symbol.
Quick Example:
let city = “New York”; // Using double quoteslet street_name = ‘E 5th Street’; // Using single quotes
Don’t be confused, In street_name the value is ‘E 5th Street’ String can have digits, Alphabets, and Symbols unless and until they are inside the quotation mark.
b. Number data type
- To represent a numerical value we use the Number data type.
- We can also define our numerical value as negative or positive, Integer, Float, hexadecimal, octal or exponential value.
- Always remember the first digit should always be an integer value in the number data type.
- And Most important do not use any quotation mark for the number data type.
- So this is how we create them:
let x = 55;
So you see we used again a variable let with variable name as x, our number value as 55 and separating them with Assignment operator equal = symbol.
Quick Example:
let ID = 2197; // positive integerlet score = 45.25; // Floatlet blood_sugar = -20; // negative integerlet Title_Color = 0x854712; // Hexadecimallet problem = 2.06e+3; // exponential
c. Boolean data type
- In Boolean, we have two values either true or false.
- Mostly this type is used for conditional statements like if-else, else-if, switch, while, do...while, But it can also be defined as a value to a variable.
- Now have a look at how it is created:
let alive = true;
So you see we used a variable let with variable name as alive and alive status value as true and separating them with Assignment operator equal = symbol
Quick example:
let online_status = true;let homework_submitted = false;
2. Composite data types
The composite data type is also known as the non-primitive data type. It can hold more collections of complex values. For example
- Array
- Object
a. Array
- In Array, we can store multiple values using a single variable.
- Every element in the array has a numerical index value, which shows the position of an element in the array.
- Arrays in javascript can have elements of different data types like String, boolean, number, object, function, and other arrays too.
Important note: The index of an array starts with 0, not from 1.
let myArray = [10,20,30,40,50];
So to create an array we first declare the variable, then inside the [ ] square brackets, there are elements separated by, comma.
Let’s have a look at an example:
// Array With the same data type
let food = [“Pizza”, “Burger”, “Fries”, “cake”];// Array with different data types.
let order = [25, “Burgers”, 50.99, true];
b. Object
- Object in javascript is a non-primitive data type that allows us to store multiple collections of data.
- For each data in an object, there is a property that is defined as a key-value pair, But these keys can always be a string and its value can be any data type like, number, string, boolean, array, function, and also other objects.
let myObj = {
“height-Cm”: 175,
“weight-lbs”: 275,
“distance-km”: 75
};
So you see we used curly brackets for our object inside that we defined property in double-quotes and assigned a value separated with : colon.
We can skip the double-quote unless we write our object as shown below:
let myObj = {
heightCM: 175,
weightLBS: 275,
distanceKM: 75
};
Quick example:
let online_shop = {
orderID: 57,
itemName: “Shoes”,
itemPrice: 399,
payment: true,
address: {
block: 45,
street: “downtown street”,
city: “cape town”,
pincode: 59595
},
ratings: [1,2,3,4,5]};
Bonus
There is also another category of data type that is:
3. Special data type
There are two special data types in javascript namely undefined and null.
a. Undefined data type
A variable that is declared but never assigned a value is undefined. Example :
let a;console.log(a); // a is undefined
b. Null data type
- A variable that has only one value that is null itself.
- This clearly states that there is not value in the variable.
- And it is not the same as empty string or 0 or undefined.
- Null is specially passed to the variable.Example:
let a = null;console.log(a); // a gives us null