JavaScript Variables - Complete Tutorial

JavaScript Variables - Complete Tutorial

Admin Feb 23, 2026 JavaScript

What are JavaScript Variables?


Variables are containers for storing data values. In JavaScript, you can declare variables using three keywords: var, let, and const.


Declaring Variables


var carName = "Volvo";

let age = 25;

const PI = 3.14;


Difference between var, let, and const:


var: Function scoped, can be redeclared and updated. This is the older way of declaring variables.

let: Block scoped, can be updated but not redeclared. Introduced in ES6.

const: Block scoped, cannot be updated or redeclared. Used for constants.


Variable Naming Rules


1. Names must start with a letter, underscore (_) or dollar sign ($)

2. Names are case sensitive

3. Names should be descriptive and meaningful

4. Use camelCase for variable names


Variable Types


JavaScript variables can hold different data types:


String: "John"

Number: 42

Boolean: true or false

Array: [1, 2, 3]

Object: {name: "John", age: 30}

Null: null

Undefined: undefined


Variable Scope


Global scope: Variables declared outside functions are globally scoped

Local scope: Variables declared inside functions have local scope

Block scope: Variables declared with let and const have block scope


Best Practices


1. Use const by default

2. Use let if the variable needs to change

3. Avoid var in modern JavaScript

4. Always declare variables before using them

5. Use meaningful variable names


Conclusion


Understanding variables is fundamental to JavaScript programming. Proper use of var, let, and const helps write cleaner, more maintainable code with fewer bugs.


Share this post:

Leave a Comment