Data Types specify the type of value that a variable can hold.
JavaScript is a Dynamically Typed Language, which means the data type of a variable is determined automatically at runtime based on the value assigned to it.
Example:
let name = "Basha"; // String
let age = 25; // Number
let status = true; // BooleanJavaScript Data Types are classified into two categories:
- Primitive Data Types
- Non-Primitive Data Types
Primitive data types are simple data types that store a single value.
The Primitive Data Types are:
- String
- Number
- Boolean
- Undefined
- Null
A String is a sequence of characters enclosed within:
- Double Quotes
" " - Single Quotes
' ' - Backticks
` `
var firstName = "John";
console.log(firstName);
console.log(typeof(firstName));
var middleName = 'Doe';
console.log(middleName);
console.log(typeof(middleName));John
string
Doe
string
The Number data type stores:
- Integer values
- Floating-point values
var age = 30;
console.log(age);
console.log(typeof(age));
var percentage = 85.5;
console.log(percentage);
console.log(typeof(percentage));30
number
85.5
number
Boolean stores only two values:
- true
- false
var isMarried = true;
console.log(isMarried);
console.log(typeof(isMarried));
var pass = false;
console.log(pass);
console.log(typeof(pass));true
boolean
false
boolean
A variable that is declared but not assigned any value is called Undefined.
var myVariable;
console.log(myVariable);
console.log(typeof(myVariable));
var anotherVariable = undefined;
console.log(anotherVariable);
console.log(typeof(anotherVariable));undefined
undefined
undefined
undefined
Null represents the intentional absence of a value.
var emptyValue = null;
console.log(emptyValue);
console.log(typeof(emptyValue));
var anotherEmptyValue = null;
console.log(anotherEmptyValue);
console.log(typeof(anotherEmptyValue));null
object
null
object
Important Note:
In JavaScript,
typeof nullreturns"object".This is a historical bug in JavaScript that has been kept for backward compatibility.
Non-Primitive Data Types are complex data structures that can store multiple values.
The Non-Primitive Data Types are:
- Object
- Array
An Object is a collection of key-value pairs.
var person = {
name: "John",
age: 30,
isMarried: true
};
console.log(person);
console.log(typeof(person));{
name: 'John',
age: 30,
isMarried: true
}
object
var anotherPerson = {
name: "Jane",
age: 25,
isMarried: false
};
console.log(anotherPerson);
console.log(typeof(anotherPerson));{
name: 'Jane',
age: 25,
isMarried: false
}
object
An Array is an ordered collection of values.
Arrays can store:
- Numbers
- Strings
- Booleans
- Objects
- Mixed Data Types
var numbers = [1, 2, 3, 4, 5];
console.log(numbers);
console.log(typeof(numbers));[1, 2, 3, 4, 5]
object
var fruits = ["apple", "banana", "orange"];
console.log(fruits);
console.log(typeof(fruits));['apple', 'banana', 'orange']
object
| Primitive | Non-Primitive |
|---|---|
| Stores Single Value | Stores Multiple Values |
| Immutable | Mutable |
| Stored by Value | Stored by Reference |
| Faster Access | Comparatively Slower |
| Examples: String, Number, Boolean | Examples: Object, Array |
- String
- Number
- Boolean
- Undefined
- Null
- Object
- Array
- JavaScript is a Dynamically Typed Language.
typeof()is used to determine the data type of a variable.- Arrays are special types of Objects.
typeof nullreturns"object"because of a historical JavaScript bug.- Primitive values are stored by value.
- Objects and Arrays are stored by reference.
JavaScript supports two categories of data types:
-
Primitive Data Types
- String
- Number
- Boolean
- Undefined
- Null
-
Non-Primitive Data Types
- Object
- Array
Understanding data types is fundamental for learning variables, operators, functions, objects, arrays, DOM manipulation, and advanced JavaScript concepts.