JavaScript Data Types

Learn JavaScript data types with detailed explanations, examples, outputs, and real-world usage.

JavaScript Data Types

Data types define the type of value stored inside a variable. JavaScript automatically identifies the type of data based on the assigned value.

Understanding data types is very important because different types of data behave differently in programs.

Why Data Types are Important?

  • Help JavaScript understand stored values
  • Define which operations can be performed
  • Improve program accuracy
  • Help avoid programming errors
  • Make data handling easier

Main JavaScript Data Types

JavaScript mainly provides the following data types:

String
Number
Boolean
Undefined
Null
Object
Array

String Data Type

Strings are used to store text values. Strings are written inside single quotes, double quotes, or backticks.

let name = "Rahul";

let city = 'Delhi';

console.log(name);

console.log(city);

Output

Rahul

Delhi

Explanation

  • Strings store textual data.
  • "Rahul" and "Delhi" are string values.
  • console.log() displays the values.

Number Data Type

Numbers are used to store integer and decimal values. JavaScript does not separate integers and floating-point numbers.

let age = 20;

let price = 99.99;

console.log(age);

console.log(price);

Output

20

99.99

Explanation

  • 20 is an integer value.
  • 99.99 is a decimal value.
  • Both are treated as Number type in JavaScript.

Boolean Data Type

Boolean values represent true or false conditions.

let isLoggedIn = true;

let isAdmin = false;

console.log(isLoggedIn);

console.log(isAdmin);

Output

true

false

Explanation

  • Boolean values are mainly used in conditions.
  • true means yes or active.
  • false means no or inactive.

Undefined Data Type

A variable that is declared but not assigned a value has the undefined type.

let userName;

console.log(userName);

Output

undefined

Explanation

  • The variable exists but has no value.
  • JavaScript automatically assigns undefined.

Null Data Type

null represents an intentionally empty value.

let data = null;

console.log(data);

Output

null

Explanation

  • null means no value is assigned intentionally.
  • It is different from undefined.

Object Data Type

Objects store multiple values in key-value format.

let student = {

    name: "Rahul",

    age: 20,

    marks: 85

};

console.log(student);

Output

{
  name: "Rahul",
  age: 20,
  marks: 85
}

Explanation

  • Objects group related data together.
  • name, age, and marks are object properties.
  • Each property has a key and value.

Array Data Type

Arrays store multiple values inside a single variable.

let colors = ["Red", "Blue", "Green"];

console.log(colors);

Output

["Red", "Blue", "Green"]

Explanation

  • Arrays store collections of values.
  • Values are separated using commas.
  • Arrays are written inside square brackets [].

Checking Data Types using typeof

JavaScript provides the typeof operator to check data types.

let name = "Ali";

let age = 20;

let passed = true;

console.log(typeof name);

console.log(typeof age);

console.log(typeof passed);

Output

string

number

boolean

Explanation

  • typeof checks the datatype of values.
  • String values return "string".
  • Numbers return "number".
  • Boolean values return "boolean".

JavaScript Dynamic Typing

JavaScript is dynamically typed, meaning variables can store different data types at different times.

let value = 100;

console.log(value);

value = "CodeVyro";

console.log(value);

Output

100

CodeVyro

Explanation

  • The variable first stores a number.
  • Later it stores a string value.
  • JavaScript allows datatype changes dynamically.

Complete Real Life Example

The following example shows multiple data types together in a student management system.

let studentName = "Rahul";

let studentAge = 20;

let studentMarks = 85.5;

let passed = true;

let address = null;

let subjects = ["Math", "Science", "English"];

console.log(studentName);

console.log(studentAge);

console.log(studentMarks);

console.log(passed);

console.log(address);

console.log(subjects);

Output

Rahul

20

85.5

true

null

["Math", "Science", "English"]

Detailed Explanation of Example

  • studentName stores string data.
  • studentAge stores number data.
  • studentMarks stores decimal values.
  • passed stores boolean data.
  • address stores null value.
  • subjects stores multiple values inside an array.

Primitive and Non-Primitive Data Types

JavaScript data types are divided into two categories.

Primitive Data Types

  • String
  • Number
  • Boolean
  • Undefined
  • Null

Non-Primitive Data Types

  • Object
  • Array
  • Functions

Best Practices

  • Use meaningful variable names.
  • Choose proper data types.
  • Use arrays for multiple values.
  • Use objects for grouped data.
  • Check data types using typeof when needed.

Important Notes

  • JavaScript automatically identifies data types.
  • Numbers include integers and decimals.
  • Strings store text values.
  • Booleans store true or false.
  • Objects and arrays store complex data.

Summary

  • Data types define the type of stored data.
  • JavaScript supports multiple data types.
  • Strings store text values.
  • Numbers store numeric values.
  • Booleans store true or false.
  • Arrays store multiple values.
  • Objects store key-value data.
  • typeof checks the datatype of values.