Javascript Data Types
A value in JavaScript is always associated with a specific data type, which determines the nature of the value. JavaScript offers various data types to accommodate different kinds of values.
Example:
var a = 20; // holding a number
var b = "Test"; // holding a string
There are two types of data types supported by JavaScript:-
Primitive data type
Primitive data types are single, simple values with no additional properties or methods. JavaScript has five primitive data types:
Data Type |
Description |
String |
represents sequence of characters e.g. "hello" |
Number |
represents numeric values e.g. 100 |
Boolean |
represents boolean value either false or true |
Undefined |
represents undefined value |
Null |
represents null i.e. no value at all |
JavaScript is a dynamic language or loosely typed therefore a variable doesn’t associate with any type. However, its value does.
In other words, the same variable can hold values of different types at any time. For example:
let counter = 20; // counter is a number
counter = false; // counter is now a boolean
counter = "test"; // counter is now a string
The string type
In JavaScript, a string is a sequence of zero or more characters. A literal string begins and ends with either a single quote(‘) or a double quote (“).
A string that starts with a double quote must end with a double quote and a string that begins with a single quote must end with a single quote.
let greeting = 'Hi';
let s = "It's a valid string";
let str = 'I'm also a string'; // use to escape the single quote (')
JavaScript strings are immutable. It means that we cannot modify a string once it is created.
However, we can create a new string based on an operation on the original string, like this:
let str = 'JavaScript';
str = str + ' String';
The number type
In JavaScript, a variable can hold a number of any type – integer or floating.
To represent a floating-point number, we include a decimal point followed by at least one number and an integer number is without any decimal point.
let f1 = 12; //Integer number
let f2 = 0.3; //Floating point number
JavaScript converts a floating-point number into an integer number if the number appears to be the whole number.
The reason is that Javascript always wants to use less memory since a floating-point value uses twice as much memory as an integer value.
The boolean type
The boolean type has two values: true and false, in lowercase. It allows values of other types to be converted into boolean values of true or false.
To convert a value of another data type into a boolean value, we use the Boolean function.
let inProgress = true;
let completed = false;
The undefined type
The undefined type is a primitive type that has only one value - undefined
.
By default, when a variable is declared but not initialized, it is assigned the value undefined
.
let counter;
console.log(counter); // undefined
console.log(typeof counter); // undefined
In the above example, the counter is a variable. Since the counter hasn’t been initialized, it is assigned the value undefined. The type of counter is also undefined.
The null type
The null type is the second primitive data type that also has only one value: null
.
It defines that null is an empty object pointer.
let obj = null;
console.log(typeof obj); // object
It is a good practice to assign a variable that later holds an object to null so that you can check whether the object is null or not by using the if statement.
Non-primitive (reference) data type
The non-primitive data types are as follows:
Data Type |
Description |
Object |
represents instance through which we can access members |
Array |
represents group of similar values |
RegExp |
represents regular expression |
The object type
In JavaScript, an object is a collection of properties, where each property is defined as a key-value pair.
The following example defines the person object with two properties.
A property name of an object can be any string. We can use quotes around the property name if it isn’t a valid JavaScript identifier.
The array type
JavaScript arrays are used to store multiple values in a single variable.
var person = ["Matt", "Jenny", "Mike"];
A property name of an object can be any string. We can use quotes around the property name if it isn’t a valid JavaScript identifier.
The regExp type
RegExp is a regular expression in which a sequence of characters is used to form a search pattern.
That search pattern can be used for text search and text replacement operations.
let re = new RegExp('hi');