In this chapter, we will learn about JavaScript variables. We will cover:
- What is a Variable?
- Declaring Variables
- Variable Types
- Variable Scope
- Variable Naming Rules
What is a Variable?
A variable is a container for storing data values. Think of it as a box where you can put information that you want to use later. In JavaScript, you can store numbers, text, objects, and more in variables.
Declaring Variables
In JavaScript, you can declare variables using var
, let
, or const
.
Using var
var
is used to declare variables that can be re-declared and updated. It has function scope, meaning it is accessible throughout the function in which it is declared.
Example:
var name = "Amit"; console.log(name); // Output: Amit var name = "Raj"; console.log(name); // Output: Raj name = "Priya"; console.log(name); // Output: Priya
Using let
let
is used to declare variables that can be updated but not re-declared within the same scope. It has block scope, meaning it is only accessible within the block (e.g., {}
) where it is declared.
Example:
let age = 25; console.log(age); // Output: 25 age = 26; console.log(age); // Output: 26 // let age = 30; // This will cause an error if uncommented
Using const
const
is used to declare variables that cannot be updated or re-declared. It also has block scope. Once a value is assigned to a const
variable, it cannot be changed.
Example:
const pi = 3.14; console.log(pi); // Output: 3.14 // pi = 3.14159; // This will cause an error if uncommented // const pi = 3.15; // This will also cause an error if uncommented
Variable Types
JavaScript variables can hold different types of data:
Numbers
Example:
let number = 10; console.log(number); // Output: 10 number = 15.5; console.log(number); // Output: 15.5
Strings
Example:
let greeting = "Namaste!"; console.log(greeting); // Output: Namaste! greeting = 'Hello, JavaScript!'; console.log(greeting); // Output: Hello, JavaScript!
Booleans
Example:
let isStudent = true; console.log(isStudent); // Output: true isStudent = false; console.log(isStudent); // Output: false
Arrays
Example:
let colors = ["red", "green", "blue"]; console.log(colors); // Output: ["red", "green", "blue"] colors[0] = "yellow"; console.log(colors); // Output: ["yellow", "green", "blue"]
Objects
Example:
let person = { firstName: "Ravi", lastName: "Kumar", age: 25 }; console.log(person); // Output: { firstName: "Ravi", lastName: "Kumar", age: 25 } person.age = 26; console.log(person); // Output: { firstName: "Ravi", lastName: "Kumar", age: 26 }
Null and Undefined
Example:
let emptyValue = null; console.log(emptyValue); // Output: null let undefinedValue; console.log(undefinedValue); // Output: undefined
Variable Scope
Scope determines the visibility of variables. JavaScript has two types of scope:
Function Scope
Variables declared with var
inside a function are function-scoped.
Example:
function myFunction() { var message = "Hello, Function Scope!"; console.log(message); // Output: Hello, Function Scope! } // console.log(message); // This will cause an error if uncommented
Block Scope
Variables declared with let
or const
inside a block (e.g., inside curly braces {}
) are block-scoped.
Example:
if (true) { let blockMessage = "Hello, Block Scope!"; console.log(blockMessage); // Output: Hello, Block Scope! } // console.log(blockMessage); // This will cause an error if uncommented
Variable Naming Rules
- Variable names must start with a letter, underscore (_), or dollar sign ($).
- Variable names can contain letters, digits, underscores, and dollar signs.
- Variable names are case-sensitive (
myVariable
andmyvariable
are different).
Example:
let _name = "Ravi"; let $age = 25; let fullName = "Ravi Kumar"; console.log(_name); // Output: Ravi console.log($age); // Output: 25 console.log(fullName); // Output: Ravi Kumar
Conclusion
In this chapter, you learned about declaring variables using var
, let
, and const
, different types of variables, variable scope, and naming rules. Understanding these basics is crucial for writing clear and efficient JavaScript code. In the next chapter, we will explore JavaScript data types and how to use them in your JavaScript programs.