Data types in Js are similar to other programming languages (C# or Java).
Javascript minimizes the need to specify the type of variable.
The following example shows how a variable can be assigned any type of value.
var testVariable = 1; // numeric value
testVariable = 'one'; // string value
testVariable = 1.1; // decimal value
testVariable = true; // Boolean value
testVariable = null; // null value
These are the lowest level of the data value in JavaScript. The typeof
operator can be used with primitive data types to know the type of a value.
Examples :
-
String
: String is a textual content wrapped inside ' ' or " " or -
Number
: Number is a numeric value. Example: 100, 4521983, etc. -
BigInt
: BigInt is a numeric value in the arbitrary precision format. Example: 453889879865131n, 200n, etc. -
Boolean
: Boolean is a logical data type that has only two values,true
orfalse
.Null
: A null value denotes an absense of value. Example:var str = null;
-
Undefined
: undefined is the default value of a variable that has not been assigned any value.
Object
: An object holds multiple values in terms of properties and methods.
Example:
var person = {
firstName: "James",
lastName: "Bond",
age: 15
};
Date
: Date object represents date & time including days, months, years, hours, minutes, seconds and milliseconds.
Example:
var today = new Date("25 July 2021");
Array
: An array stores multiple values using special syntax.
Example:
var nums = [1, 2, 3, 4];