JavaScript Data Types
The data type of a variable defines the kind of data it can store, and manipulate within a program.
JavaScript supports three categories of data types as mentioned below.
Category | Description | Data Types |
Primitive Data Types | These data types can hold only one value at a time. | String, Number, Boolean |
Composite Data Types | These data types can hold a collection of values and are more complex in nature. | Object, Array, Function |
Special Data Types | Undefined and Null are special data types. | Undefined, Null |
String Data Type
The string data type is used to store textual data, which can be a sequence of characters.
- The string values must be quoted with single or double quotes.
- In case the string value contains single or double quotes, make sure to escape such characters.
Basic String Assignements.
var a = 'Hello World!'; // Using single quotes.
var b = "Hello World!"; // Using double quotes.
String values containing quotes.
var c = "Let's plan for a trip."; // Using single quote inside double quotes.
var d = 'He said "Hello" and left.'; // Using double quotes inside single quotes.
var e = 'We\'ll never give up.'; // Escaping single quote with a backslash.
For more details about strings, check JavaScript Strings.
Number Data Type
The number data type is used to store numeric data, which can be positive, negative, decimal, and exponential values.
- The number values must not be quoted.
- The values
Infinity
and-Infinity
are special numeric values, greater than any number, which is a result of dividing a non-zero number by a zero. - The value
NaN
is another special numeric value, which is a result of an invalid or undefined mathematical operation.
Basic Number Assignments.
var a = 25; // Assigning integer value
var b = 80.5; // Assigning floating-point number
var c = 4.25e+6; // Assigning exponential notation, same as 4.25e6 or 4250000
var d = 4.25e-6; // Assigning exponential notation, same as 0.00000425
Expressions that return Infinity values.
alert(10 / 0); // Output: Infinity
alert(-10 / 0); // Output: -Infinity
alert(10 / -0); // Output: -Infinity
Expressions that return NaN (Not a Number) value.
alert("Some text" / 2); // Output: NaN
alert("Some text" / 2 + 10); // Output: NaN
alert(Math.sqrt(-1)); // Output: NaN
For more details about strings, check JavaScript Numbers.
Boolean Data Type
The boolean data type can hold only two values, true or false, which can be used to store values that can have only two states.
- It is used to store variables that can have only two states, like yes/no, on/off, or true/false.
- It can also be used to return the result of a logical operation.
Basic Boolean Assignments.
var isReading = true; // yes, I'm reading
var isSleeping = false; // no, I'm not sleeping
Comparison results that return boolean values.
var a = 1, b = 5, c = 10;
console.log(b > a) // Output: true
console.log(b > c) // Output: false
Object Data Type
The object data type is one among the composite data types, which is complex in nature, as it can store collections of data.
- Object contains properties, defined as key-value pairs.
- The property key (or name) is always a string with quotes around it, and the quotes can be omitted if the name is a valid JavaScript name.
- The property value can be of any data type, like string, number, boolean, array, function, or any other object.
Declare an empty object.
var emptyObject = {};
Declare an object with properties.
var user = { "name": "Jack", "address": "123 ABC Street", "age": 25, "isMarried": false };
Declare an object with properties (with proper indentation) for better reading.
// For better reading
var user = {
"name": "Jack",
"address": "123 ABC Street",
"age": 25,
"isMarried": false
}
Declare an object with properties (without quotes) as the property names are valid.
- If the property name "isMarried" is renamed to "is-married", we must use the quotes as the property name becomes invalid in JavaScript due to hyphen.
// Omitting quotes around the property names, as the property names are valid.
var user = {
name: "Jack",
address: "123 ABC Street",
age: 25,
isMarried: false
}
For more details about objects, check JavaScript Objects.
Array Data Type
The array data type is one among the composite data types, which can store a collection of data.
- Each array element is also called an array item.
- Array elements can be retrieved using their index, which starts with 0 (zero) for the first element.
- The array elements can be of any data type, like string, number, boolean, functions, objects, and even other arrays.
Create an empty object.
var fruits = [];
Create an array with some elements, where each of them can be retrieved using their index.
var fruits = [ "Apple", "Mango", "Banana", "Grapes" ];
console.log("First array element: " + fruits[0]);
console.log("Second array element: " + fruits[1]);
For more details about arrays, check JavaScript Arrays.
Function Data Type
The function data type is one among the composite data types, which executes a block of code.
- Functions are treated as objects, so they can be assigned to variables.
- Functions can be stored in variables, objects, and arrays.
- Functions can be passed as arguments to other functions.
- Functions can be returned from other functions.
Function assigned to a variable.
// Function is assigned to a varialbe
var greeting = function(){
return "Hello World!";
}
// Check the type of greeting variable
console.log(typeof greeting) // Output: function
console.log(greeting()); // Output: Hello World!
Function passed as a parameter to other function, and returned from other function.
function greeting(){
return "Hello World!";
}
function displayGreeting(greetingFunction){ // Here, the greetingFunction() is nothing by greeting() function.
return greetingFunction(); // Here, the function is returned from other function.
}
var greet = displayGreeting(greeting); // Here, the greeting() function is passed as a parameter to other function.
console.log("Greeting message: " + greet); // Output: Hello World!
For more details about functions, check JavaScript Functions.
Undefined Data Type
This is a special data type that can have only one value (i.e. undefined), which means the value is not assigned to a variable.
var a;
var b = "Hello World!";
console.log(a) // Output: undefined
console.log(b) // Output: Hello World!
Null Data Type
This is a special data type that can have only one value (i.e. null), which means the null value is explicitly assigned to a variable.
- The null value is not equal to the empty string
""
or0
(zero). - The null value means no value.
- It can be used to explicitly empty a variable value by assigning it to a null value.
Declare a variable with null value.
var a = null;
console.log(a); // Output: null
Assign null value to a variable to empty its value.
var a = "Hello World!";
console.log(a); // Output: Hello World!
a = null;
console.log(a); // Output: null
Finding Data Type of a Variable
The JavaScript operator typeof
can be used to find the data type of a variable.
- There may be instances where we need to find the data type of a variable before we process the values within a program. In all such instances, the operator
typeof
is very useful. - The operator can be used with or without parentheses (like
typeof(x)
ortypeof x
).
Data type of string values return string.
// Strings
typeof ''; // Returns: "string"
typeof 'hello'; // Returns: "string"
typeof '12'; // Returns: "string". Number within quotes is considered a string.
Data type of numeric values return number.
// Numbers
typeof 15; // Returns: "number"
typeof 42.7; // Returns: "number"
typeof 2.5e-4; // Returns: "number"
typeof Infinity; // Returns: "number"
typeof NaN; // Returns: "number". Despite being "Not-A-Number"
Data type of boolean values return boolean.
// Booleans
typeof true; // Returns: "boolean"
typeof false; // Returns: "boolean"
Data type of objects return object.
// Objects
typeof {name: "Jack", age: 25}; // Returns: "object"
Data type of arrays return array.
// Arrays
typeof [1, 2, 3, 4, 5]; // Returns: "object"
Data type of functions return funtion.
// Functions
typeof function(){}; // Returns: "function"
Data type of undefined values return undefined.
// Undefined
typeof undefined; // Returns: "undefined"
typeof undeclaredVariable; // Returns: "undefined"
Data type of null values return object, instead of null.
- The type of null value returns object, which is an outstanding, well-known bug in JavaScript, which has been rejected to fix by the ECMA committee that maintains JavaScript.
// Null
typeof Null; // Returns: "object"
For more details on data type conversions, check JavaScript Type Conversions.
Overall
JavaScript data types define the values a variable can store.