0% found this document useful (0 votes)
4 views2 pages

JavaScript Variables Explained

JavaScript variables are containers for storing data values, with three types: var (function-scoped), let (block-scoped), and const (block-scoped constant). Variable names must follow specific rules, including starting with a letter, underscore, or dollar sign, and cannot be reserved keywords. Key concepts include variable scope, hoisting, re-declaration, and best practices for naming and using variables.

Uploaded by

deepdid32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

JavaScript Variables Explained

JavaScript variables are containers for storing data values, with three types: var (function-scoped), let (block-scoped), and const (block-scoped constant). Variable names must follow specific rules, including starting with a letter, underscore, or dollar sign, and cannot be reserved keywords. Key concepts include variable scope, hoisting, re-declaration, and best practices for naming and using variables.

Uploaded by

deepdid32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Variables – Full Information

In JavaScript, variables are containers used to store data values. A variable allows you to label data
with a descriptive name, making your programs easier to understand.

Types of Variables
1 var – Function-scoped variable. Can be re-declared and updated. Hoisted with undefined
value.

2 let – Block-scoped variable. Cannot be re-declared within same block but can be updated.
3 const – Block-scoped constant. Must be assigned at declaration and cannot be re-assigned.

Rules for Naming Variables


1 Variable names must begin with a letter, underscore (_), or dollar sign ($).
2 Variable names are case-sensitive (myVar ≠ Myvar).
3 Cannot use reserved JavaScript keywords as variable names.

Variable Scope
Scope determines where a variable can be accessed in a program.

1 Global Scope: Variables declared outside any function are global.


2 Function Scope: Variables declared with var inside a function are only available within that
function.

3 Block Scope: Variables declared with let and const inside { } are only available within that
block.

Hoisting
JavaScript moves variable and function declarations to the top of their scope before code
execution. `var` variables are hoisted with an initial value of undefined, while `let` and `const` are
hoisted but not initialized (temporal dead zone).

Re-declaration & Re-assignment


1 `var` – Can be re-declared and updated.
2 `let` – Cannot be re-declared but can be updated.
3 `const` – Cannot be re-declared or updated.

Best Practices
1 Use `const` by default, and `let` when reassignment is required.
2 Avoid using `var` in modern JavaScript.
3 Use meaningful and descriptive variable names.
4 Keep variable scope as small as possible.

Example Code
let name = "Shubham"; const age = 23; var city = "Ludhiana"; function displayInfo()
{ let message = `Name: ${name}, Age: ${age}, City: ${city}`; [Link](message); }
displayInfo();

You might also like