Data Types In JavaScript
Martin Wachira
Posted on January 27, 2023
A data type, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
Basic data types in JavaScript include:
- Number
- String
- Boolean
- Symbol
- Null
- Undefined
- Object
The above data types are categorized into two categories:
1.Primitive data types, these are data types that store a single value. They include number, String, Boolean, Symbol, Null and Undefined.
2.Non primitive (Reference) data types, these data types are derived from primitive data types and have properties and methods to manipulate their values. An example is the object data type.
Difference between Primitive and non-primitive Data types
1.Primitive data types are compared by value, that is, two values are strictly equal if they have the same value.
Sample
let age = 18;
let num = 18;
let isEqual = age === num;
console.log(isEqual); //Output -> true
Non-primitive data types are compared by reference, that is, two objects are strictly equal if they refer to the same values.
let sports1 = ['Football', 'Rugby', 'Basketball'];
let sports2 = sports1;
let isEqual = sports1 === sports2;
console.log(isEqual); //Output -> true
Primitive Data types
Primitive data types are those that hold a single value.
let x = 5
let y = 12.5
They include:
1. Number
It can be an integer, a float , an exponential, a NaN and an infinity.
let x = 100; //An integer
let y = 12.43; //A float
let z = 10e4; //An exponential
NaN(Not a Number) is used to represent an invalid number, results when you try to perform an operation on a number with a non-numeric value.
let b = 12 * 'f';
console.log(b); //Output -> NaN
Infinity, it results when we divide a number by zero
let b = 12 / 0;
console.log(b); //Output -> Infinity
2.Boolean
Holds a true or false value.
It is mostly used to check if a logical condition is true or false.
let isGreater = 3 < 9;
console.log(isGreater); //Output -> true
3.Null
Is a special value that represents an unknown value.
let b = null;
console.log(b); //Output -> null
4.Undefined
It represents a value that is not defined.
let b;
console.log(b); //Output -> undefined
5.String
A string is any group of characters surrounded by quotes, double quotes or backticks.
When using double quotes or single quotes, there is no practical difference between them.
let country = "Kenya";
let city = 'Nairobi';
A backtick is mostly used when appending a variable to a string
let backtick = 'backtick';
console.log (`This is a ${backtick}`);
//Output -> This is a backtick
Non-Primitive Data Types
1.Objects
In JavaScript, an object is a collection of properties where each property is defined with a key-value pair.
let obj = {
country: 'Kenya',
capital: 'Nairobi',
continent: 'Africa'
}
The above object has three properties country, capital and continent with corresponding values of Kenya, Nairobi and Africa.
How to access data in objects
To access values in an object, we use the dot notation and bracket notation.
Dot notation
objectName.propertyName //Syntax to access values of an object
let countryName = obj.country;
console.log(countryName); //Output -> Kenya
let capitalName = obj.capital;
console.log(capitalName); //Output -> Nairobi
Bracket Notation
objectName['propertyName'] //Syntax to access values of an object
let countryName = obj['country'];
console.log(countryName); //Output -> Kenya
let capitalName = obj['capital'];
console.log(capitalName); //Output -> Nairobi
Adding a value in an object
obj.population = '51.09 million';
console.log(obj);
// Output
{
country: 'Kenya',
capital: 'Nairobi',
continent: 'Africa',
population: '51.09 million'
}
Delete a value in the object
delete obj.population;
console.log(obj);
//Output
{
country: 'Kenya',
capital: 'Nairobi',
continent: 'Africa',
}
Modifying value of a property in an object
let intialObj =
{
country: 'Kenya',
capital: 'Nairobi',
continent: 'Africa',
population: '51.09 million'
}
intialObj.country = 'Tanzania';//Changes the value Kenya to Tanzania
console.log(intialObj);
//Output
{
country : 'Tanzania',
capital : 'Nairobi',
continent :'Africa',
population : '51.09 million'
}
Posted on January 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.