Skip to main contentJavaScript Data Types Tutorial - HTML5ANDCSS3JavaScript Basics
JavaScript Data Types
Written by Swapnil RajaPublished JavaScript data types define what kind of value a variable can store.
A variable can store text, numbers, true or false values, lists, objects, and more.
Example
let name = "Amit";
let age = 25;
let isStudent = true;
In the above example:
<code>name</code> | "Amit" | String |
<code>age</code> | 25 | Number |
<code>isStudent</code> | true | Boolean |
Each variable stores a different type of value, and JavaScript treats those values differently.
Why Data Types Matter
Data types are important because JavaScript handles different values in different ways.
For example, JavaScript adds numbers, but it joins strings together.
let a = 10;
let b = 20;
console.log(a + b);
Here, JavaScript adds the numbers and gives the result 30.
let a = "10";
let b = "20";
console.log(a + b);
Here, JavaScript joins the values and gives the result 1020.
So, understanding data types helps you avoid wrong results in your code.
Main JavaScript Data Types
JavaScript has several data types.
| Data Type | Description | Example |
|---|
| String | Text value | "Hello" |
| Number | Numeric value | 25 |
| Boolean | True or false value | true |
| Undefined | Variable declared without value | undefined |
| Null | Empty value | null |
| Object | Collection of related data | { name: "Amit", age: 25 } |
| Array | List of values | ["HTML", "CSS", "JavaScript"] |
| BigInt | Very large integer | 123456789n |
| Symbol | Unique value | Symbol("id") |
For beginners, String, Number, Boolean, Undefined, Null, Object, and Array are the most important.
String
A string is used to store text.
Strings are written inside quotes.
let firstName = "Amit";
let course = "JavaScript";
You can use single quotes or double quotes.
let city = 'Delhi';
let country = "India";
Number
A number is used to store numeric values.
let age = 25;
let price = 499.99;
JavaScript uses the same Number type for both whole numbers and decimal numbers.
let a = 10;
let b = 5;
let total = a + b;
Boolean
A boolean has only two values:
Booleans are mostly used in conditions.
let isLoggedIn = true;
let isAdmin = false;
Boolean values are useful when you want to check whether something is yes or no, active or inactive, correct or incorrect.
Undefined
A variable is undefined when it is declared but no value is assigned.
let message;
console.log(message);
Here, message exists, but it has no value yet.
Null
null means empty or no value.
It is used when you intentionally want to keep a variable empty.
Here, selectedUser has no value right now.
Difference between undefined and null:
| Value | Meaning |
|---|
<code>undefined</code> | Value is not assigned |
<code>null</code> | Empty value is assigned intentionally |
Object
An object is used to store related data in one variable.
let student = {
name: "Amit",
age: 20,
course: "JavaScript"
};
You can access object values using property names.
console.log(student.name);
console.log(student.course);
Objects are useful when you want to store information about one thing, such as a student, user, product, or book.
Array
An array is used to store multiple values in a single variable.
let languages = ["HTML", "CSS", "JavaScript"];
Array values are accessed using index numbers.
console.log(languages[0]);
console.log(languages[1]);
console.log(languages[2]);
Array index starts from 0.
| Index | Value |
|---|
<code>0</code> | HTML |
<code>1</code> | CSS |
<code>2</code> | JavaScript |
BigInt
BigInt is used for very large integer numbers.
let bigNumber = 12345678901234567890n;
The n at the end makes it a BigInt value.
For most beginner-level programs, normal numbers are enough.
Symbol
A Symbol is a unique value.
Symbol is mostly used in advanced JavaScript, so beginners do not need to use it much in the beginning.
typeof Operator
The typeof operator is used to check the data type of a value.
let name = "Amit";
let age = 25;
let isStudent = true;
console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);
The typeof operator is very useful when you want to check what type of value a variable contains.
Complete Example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Data Types</title>
</head>
<body>
<h1>JavaScript Data Types</h1>
<p id="result"></p>
<script>
let name = "Amit";
let age = 25;
let isStudent = true;
let course;
let selectedBook = null;
let skills = ["HTML", "CSS", "JavaScript"];
let student = {
name: "Rahul",
age: 20
};
document.getElementById("result").innerHTML =
"Name: " + name + "<br>" +
"Age: " + age + "<br>" +
"Student: " + isStudent + "<br>" +
"Course: " + course + "<br>" +
"Selected Book: " + selectedBook + "<br>" +
"Skill: " + skills[2] + "<br>" +
"Object Name: " + student.name;
</script>
</body>
</html>
Try It Yourself
Run the above example in the Try It Editor.
Try changing the values of name, age, isStudent, skills, and student.
Also try using the typeof operator with different variables.
Important Notes
- JavaScript variables can store different types of values.
- Strings are written inside quotes.
- Numbers are written without quotes.
- Boolean values are either true or false.
- A variable without a value is undefined.
- null is used when you intentionally want to store an empty value.
- Objects store related data in key-value pairs.
- Arrays store multiple values in a list.
- The typeof operator is used to check data types.
Conclusion
JavaScript data types define the type of value stored in a variable.
The main JavaScript data types are String, Number, Boolean, Undefined, Null, Object, Array, BigInt, and Symbol.
Understanding data types is important because it helps you write correct JavaScript code and avoid unexpected results.