-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
JavaScript data types define the kind of values that a variable can store and how those values behave during operations. Understanding data types is one of the most important concepts in JavaScript because every program works with data in some form. Whether you are building a simple calculator, a form validation system, or a full-scale web application, knowing how JavaScript handles different types of data helps you write reliable and predictable code.
In this chapter, you will learn what data types are, why they matter, the different types available in JavaScript, how values behave with each type, practical examples, common pitfalls, best practices, and real-world usage scenarios.
A data type tells JavaScript what kind of value a variable holds. For example, a variable may store a number, a text value, a boolean result, or a collection of values. JavaScript is a dynamically typed language, which means you do not need to specify the data type when declaring a variable. The type is automatically determined based on the value assigned to it.
let age = 22;
let name = "Ananya";
let isStudent = true;
In this example, JavaScript automatically understands that age is a number, name is a string, and isStudent is a boolean.
Data types help JavaScript decide how to handle values during operations. Adding two numbers produces a numeric result, while adding two strings joins them together. Using the wrong type can lead to unexpected results or bugs.
Understanding data types allows you to:
Perform correct calculations
Validate user input properly
Avoid runtime errors
Write cleaner and more predictable code
Debug issues more efficiently
JavaScript data types are broadly divided into two categories:
Primitive data types
Non-primitive or reference data types
Each category serves a different purpose and behaves differently in memory.
Primitive data types store a single simple value. They are immutable, meaning their values cannot be changed directly. JavaScript has seven primitive data types.
The number data type is used to store numeric values, including integers and floating-point numbers. JavaScript does not have separate types for integers and decimals.
let ageAnanya = 22;
let heightMira = 5.4;
let temperature = -10;
Numbers can be used in arithmetic operations like addition, subtraction, multiplication, and division.
let totalMarks = 450;
let subjects = 5;
let averageMarks = totalMarks / subjects;
Special numeric values also exist, such as Infinity, -Infinity, and NaN (Not a Number).
The string data type is used to store text. Strings are enclosed in single quotes, double quotes, or backticks.
let firstName = "Ishita";
let lastName = 'Sharma';
let fullName = firstName + " " + lastName;
Strings can include letters, numbers, spaces, and symbols. JavaScript provides many built-in methods to work with strings, such as finding length, converting case, or extracting parts of a string.
The boolean data type stores one of two values: true or false. Booleans are commonly used in conditions and decision-making.
let isLoggedIn = true;
let hasPermission = false;
Booleans are often the result of comparisons.
let ageRiya = 18;
let isAdult = ageRiya >= 18;
The undefined data type means a variable has been declared but not assigned a value.
let score;
console.log(score); // undefined
This often happens when a value is expected but has not been provided yet.
The null data type represents an intentional absence of value. It is assigned explicitly by the programmer.
let selectedCourse = null;
Although null looks similar to undefined, it is used deliberately to indicate that a variable has no value.
The BigInt data type is used to store very large numbers that exceed the safe limit of the number type.
let bigNumber = 123456789012345678901234567890n;
BigInt values are useful in applications dealing with large integers such as financial systems or cryptography.
The symbol data type is used to create unique identifiers. Each symbol is unique, even if they have the same description.
let id1 = Symbol("id");
let id2 = Symbol("id");
Symbols are commonly used in advanced JavaScript scenarios such as object property keys that should not conflict with others.
Non-primitive data types can store multiple values or complex structures. These types are mutable, meaning their contents can be changed.
The object data type is used to store data in key-value pairs.
let student = {
name: "Sanya",
age: 21,
course: "JavaScript"
};
Objects are widely used to represent real-world entities and structured data.
Arrays are a special type of object used to store ordered lists of values.
let students = ["Ananya", "Ishita", "Mira", "Riya"];
Arrays allow you to store multiple values in a single variable and access them using indexes.
Functions are reusable blocks of code and are also treated as objects in JavaScript.
function greet(name) {
return "Hello " + name;
}
Functions can be assigned to variables, passed as arguments, and returned from other functions.
Primitive data types are stored by value. When you assign a primitive value to another variable, a copy is created.
let age1 = 20;
let age2 = age1;
age2 = 25;
Changing age2 does not affect age1.
Non-primitive data types are stored by reference. When you assign an object or array to another variable, both variables point to the same memory location.
let student1 = { name: "Diya", age: 22 };
let student2 = student1;
student2.age = 25;
Here, changing student2 also changes student1.
Confusing null and undefined
Expecting JavaScript to throw errors for type mismatches
Forgetting that arrays and objects are reference types
Assuming all numbers are safe without checking limits
Use meaningful variable names to indicate expected data type
Initialize variables whenever possible
Be careful when copying objects and arrays
Validate user input before processing
Understand type behavior during operations
JavaScript data types are used everywhere in web development:
Numbers for calculations, prices, and scores
Strings for names, messages, and form input
Booleans for conditions and permissions
Arrays for lists of items or users
Objects for structured data like profiles and settings
JavaScript data types define how values are stored and manipulated. Primitive types handle simple values, while non-primitive types manage complex data structures. Understanding how each data type works, how they behave in memory, and when to use them is essential for writing effective and bug-free JavaScript code.
Q1. How do you declare a string variable named greeting with the value "Hello World"?
Q2. How do you declare a number variable price with the value 199.99?
Q3. How do you check if a variable isActive is a boolean using the typeof operator?
Q4. How do you define an array fruits containing "apple", "banana", and "mango"?
Q5. How do you create an object person with properties name: "John" and age: 30?
Q6. How do you define a variable discount with the value null and check its type using typeof?
Q7. How do you define a function greet() that prints "Hi" and check its type?
Q8. How do you assign a BigInt value 12345678901234567890n to a variable bigNum?
Q9. How do you declare an undefined variable named result?
Q10. How do you create a variable now and assign the current date and time using Date() object?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
