HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Data Types

Data types describe the different kinds of data that we can work with and store in variables. In JavaScript, there are eight basic data types, which are divided into two categories: Primitive and Object (Reference) types.


The Concept of Dynamic Typing

JavaScript is a "dynamically typed" language. This means you don't have to declare the type of data a variable will hold, and the same variable can hold different types of data at different times.

let x;           // x is currently undefined
x = 5;           // x is now a number
x = "Mim";       // x is now a string 

1. Primitive Data Types

Primitive values are single pieces of data. They are immutable (cannot be changed). There are 7 primitive types in JavaScript:

String

Textual data wrapped in quotes.
Ex: "Hello"

Number

Integers and decimals.
Ex: 42, 3.14

Boolean

Logical values.
Ex: true, false

Undefined

A variable that has not been assigned a value.

Null

Represents an intentional "empty" or "nothing" value.

BigInt

Very large integers beyond the limit of Number type.

Symbol

Unique and immutable values used as object keys.


2. Non-Primitive (Reference) Type

There is only one non-primitive type in JavaScript: The Object.

Objects can store collections of data and more complex entities. Common objects you will use include:

  • Regular Objects{ name: "Mim", age: 25 }
  • Arrays[1, 2, 3, 4]
  • Datesnew Date()

Why Knowing Data Types is Important

When you use operators like +, JavaScript behaves differently based on the data type:

let x = 5 + 5;       // Result is 10 (Addition)
let y = "5" + 5;     // Result is "55" (Concatenation)
let z = "5" + "5";   // Result is "55" (Concatenation)
Note: JavaScript usually evaluates expressions from left to right. If a string is involved in an addition, the entire result often becomes a string.

Key Points to Remember

  • JavaScript variables can hold any data type (dynamic typing)
  • There are 7 primitive types: string, number, bigint, boolean, undefined, null, symbol
  • There is 1 non-primitive type: Object
  • Arrays and Dates are specific types of objects
  • Types matter when performing calculations and joining data