- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Data Types
JavaScript Basics
JavaScript Data Types
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
Example
javascript
let name = "Amit";
let age = 25;
let isStudent = true;In the above example:
| Variable | Value | Data Type |
|---|---|---|
name | "Amit" | String |
age | 25 | Number |
isStudent | 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.
Example with numbers:
Example
javascript
let a = 10;
let b = 20;
console.log(a + b);Here, JavaScript adds the numbers and gives the result 30.
Example with strings:
Example
javascript
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.
Example
javascript
let firstName = "Amit";
let course = "JavaScript";You can use single quotes or double quotes.
Example
javascript
let city = 'Delhi';
let country = "India";Both are valid.
Number
A number is used to store numeric values.
Example
javascript
let age = 25;
let price = 499.99;JavaScript uses the same Number type for both whole numbers and decimal numbers.
Example
javascript
let a = 10;
let b = 5;
let total = a + b;Boolean
A boolean has only two values:
Syntax
javascript
true
falseBooleans are mostly used in conditions.
Example
javascript
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.
Example
javascript
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.
Example
javascript
let selectedUser = null;Here, selectedUser has no value right now.
Difference between undefined and null:
| Value | Meaning |
|---|---|
undefined | Value is not assigned |
null | Empty value is assigned intentionally |
Object
An object is used to store related data in one variable.
Example
javascript
let student = {
name: "Amit",
age: 20,
course: "JavaScript"
};You can access object values using property names.
Example
javascript
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.
Example
javascript
let languages = ["HTML", "CSS", "JavaScript"];Array values are accessed using index numbers.
Example
javascript
console.log(languages[0]);
console.log(languages[1]);
console.log(languages[2]);Array index starts from 0.
| Index | Value |
|---|---|
0 | HTML |
1 | CSS |
2 | JavaScript |
BigInt
BigInt is used for very large integer numbers.
Example
javascript
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.
Example
javascript
let id = Symbol("id");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.
Example
javascript
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
Complete Example
html
<!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.
