JavaScript Data Types
Learn the different data types available in JavaScript, including strings, numbers, booleans, undefined, null, BigInt, Symbol, and objects, with practical examples and expected outputs.
1. What Are Data Types?
A data type describes the kind of value stored or processed by a program. JavaScript uses data types to determine how values behave during operations and calculations.
For example, a student's name is usually stored as a string, marks can be stored as a number, and a result can be represented using a boolean value.
let can hold a value of one type and later be
reassigned a value of another type.
2. Classification of JavaScript Data Types
JavaScript data types are broadly classified into primitive types and objects.
JavaScript Data Types
All values used in JavaScript programs
Primitive Types
String, Number, Boolean, Undefined, Null, BigInt, and Symbol
Object Type
Objects, arrays, functions, and other object values
Note: Functions are callable objects, and arrays are specialized objects in JavaScript.
3. String Data Type
A string represents text. JavaScript strings can be written using single quotes, double quotes, or backticks.
Example
let studentName = "Samadhan";
let course = 'JavaScript';
let message = `Welcome to ${course}`;
console.log(studentName);
console.log(course);
console.log(message);
Output
Samadhan
JavaScript
Welcome to JavaScript
Explanation:
The first two variables store text. The third example uses a
template literal, which allows the value of course
to be inserted using ${course}.
4. Number Data Type
The Number type represents integers and floating-point numbers. JavaScript uses the same Number type for both.
Example
let age = 22;
let price = 149.50;
console.log(age);
console.log(price);
console.log(typeof age);
console.log(typeof price);
Output
22
149.5
number
number
Explanation:
Both variables use the Number type. JavaScript does not have
separate primitive types for ordinary integers and decimals.
The console may display 149.50 as 149.5
because trailing zeros are not significant in a numeric value.
5. Boolean Data Type
A boolean represents one of two values: true
or false. Booleans are commonly used in conditions
and decision-making statements.
Example
let isStudent = true;
let hasCompletedCourse = false;
console.log(isStudent);
console.log(hasCompletedCourse);
console.log(typeof isStudent);
Output
true
false
boolean
Boolean values are useful when checking whether a student passed, whether a user is logged in, or whether a condition is satisfied.
6. Undefined Data Type
A variable declared without assigning a value has the value
undefined. It can also be assigned explicitly.
Example
let result;
console.log(result);
console.log(typeof result);
Output
undefined
undefined
Explanation:
The variable result is declared but no value is
assigned to it. Therefore, its value is undefined,
and typeof result also returns the string
"undefined".
7. Null Data Type
The value null is used to represent an intentional
absence of a value. It can be useful when a value is not currently
available or has deliberately been cleared.
Example
let selectedCourse = null;
console.log(selectedCourse);
console.log(typeof selectedCourse);
Output
null
object
typeof null returns "object" because
of a historical JavaScript behavior. However, null
is a primitive value, not an object.
8. BigInt Data Type
BigInt is used to represent integers larger than the range in which
JavaScript Number can represent every integer precisely.
A BigInt literal can be written by adding n
at the end of an integer.
Example
let largeNumber = 9007199254740993n;
console.log(largeNumber);
console.log(typeof largeNumber);
Output
9007199254740993n
bigint
BigInt and Number should not be mixed directly in ordinary arithmetic operations. Convert values carefully when working with both types.
9. Symbol Data Type
Symbol creates a unique primitive value. Symbols are often used as property keys when a unique identifier is required.
Example
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2);
console.log(typeof id1);
Output
false
symbol
Although both symbols use the same description, each call to
Symbol() creates a distinct symbol value.
10. Object Data Type
An object stores related information as properties and values. Objects are commonly used to represent students, employees, products, and other entities in applications.
Example: Object
let student = {
name: "Samadhan",
age: 22,
course: "JavaScript"
};
console.log(student.name);
console.log(student.age);
console.log(student.course);
console.log(typeof student);
Output
Samadhan
22
JavaScript
object
Example: Array
let courses = ["JavaScript", "Node.js", "React"];
console.log(courses[0]);
console.log(courses.length);
console.log(typeof courses);
console.log(Array.isArray(courses));
Output
JavaScript
3
object
true
Arrays store ordered collections of values. JavaScript reports
their type as "object", so
Array.isArray() is a useful way to check whether
a value is an array.
11. Understanding the typeof Operator
The typeof operator returns a string indicating
the type category of a value. It is useful when inspecting
values during development.
Example
console.log(typeof "Hello");
console.log(typeof 100);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 100n);
console.log(typeof Symbol("id"));
console.log(typeof {});
console.log(typeof function () {});
Output
string
number
boolean
undefined
bigint
symbol
object
function
Remember:
typeof has special cases, including
typeof null === "object". Use the appropriate
checks for arrays, null values, and other special cases.
12. Practice Task
Student Information Program
Create a JavaScript program that:
- Stores a student's name as a string.
- Stores the student's age and marks as numbers.
- Stores the pass status as a boolean.
- Declares one variable without assigning a value.
- Creates an object containing student details.
- Uses
typeofto inspect different values. - Uses
Array.isArray()to check an array.
Important Points
- JavaScript has seven primitive types.
- String represents text, Number represents ordinary numeric values, and Boolean represents true or false.
- Undefined commonly indicates an unassigned value; null represents an intentional absence of value.
- BigInt supports large integers, while Symbol creates unique primitive values.
- Objects can group related information into properties.
- Arrays and functions are object-related values with special behavior.
typeof nullreturns"object"because of a historical JavaScript behavior.
Summary
JavaScript provides seven primitive types: string, number, boolean, undefined, null, bigint, and symbol. Objects store related properties and values, while arrays represent ordered collections. The typeof operator helps inspect values, but special cases such as null and arrays require additional understanding. Knowing data types helps us write correct calculations, conditions, and application logic.