JavaScript Variables and Data Types

Subject: JavaScript

Variables in JavaScript are used to store data that can be used and modified throughout a program. Each variable holds a specific data type, such as numbers, strings, or objects, that defines what kind of value it stores.

Declaring Variables in JavaScript

JavaScript allows you to declare variables using three keywords:

  • var – (Old way) function-scoped
  • let – block-scoped (preferred for modern JS)
  • const – block-scoped and cannot be reassigned

Syntax:

Example:


Rules for Naming Variables

  • Must begin with a letter, $, or _
  • Case-sensitive (e.g., Name and name are different)
  • Cannot use reserved keywords like let, class, function

JavaScript Data Types

JavaScript supports two categories of data types:

1. Primitive Data Types

These are basic and immutable:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • Symbol (ES6+)
  • BigInt (ES2020)

2. Non-Primitive (Reference) Data Types

These include more complex structures:

  • Object
  • Array
  • Function

Example: Using Different Data Types


Key Takeaways

  • Use let and const for declaring modern JavaScript variables
  • JavaScript is dynamically typed—no need to specify the type explicitly
  • Primitive types store simple data; reference types store complex structures
  • Use typeof to check the type of a variable
  • Choose the right data type based on the value your variable should hold
Next : JS let