Related Topics
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36

JAVASCRIPT
function exampleFunction() {
if (true) {
var x = 10; // Function-scoped variable
let y = 20; // Block-scoped variable
console.log(x); // Output: 10
console.log(y); // Output: 20
}
console.log(x); // Output: 10 (var is accessible outside the block)
// console.log(y); // Error: y is not defined (let is block-scoped)
}
exampleFunction();
// Redeclaration example
var a = 5;
var a = 10; // This is allowed with var
let b = 15;
// let b = 20; // Error: Identifier 'b' has already been declared
As a best practice, it is recommended to use let
over var
in modern JavaScript development. let
provides block scoping, which can help prevent common bugs caused by variable hoisting and redeclaration. Additionally, using const
for variables that don’t need to be reassigned can further improve code readability and maintainability.
const PI = 3.14159;
const MAX_WIDTH = 800;
2. Block-Scoped Variables: Similar to let
, variables declared with const
are block-scoped, meaning they are only accessible within the block in which they are defined. This helps avoid variable hoisting issues associated with var
.
if (true) {
const message = "Hello!";
console.log(message); // Output: Hello!
}
// console.log(message); // Error: message is not defined
3. Preventing Reassignment: Once a value is assigned to a const
variable, you cannot reassign it. Attempting to do so will result in a TypeError
.
const age = 30;
// age = 40; // Error: Assignment to constant variable.
4. Immutability for Objects and Arrays: When declaring an object or array with const
, you can’t reassign the variable itself, but you can still modify its properties or elements. This means the reference to the object or array is constant, but its contents can be changed.
const person = {
name: "Alice",
age: 30,
};
person.age = 31; // This is allowed
// person = { name: "Bob" }; // Error: Assignment to constant variable.
To achieve full immutability for objects and arrays, you can use techniques like the spread operator or the Object.freeze()
method.
const immutablePerson = Object.freeze({
name: "Alice",
age: 30,
});
As a best practice, use const
by default for variables that won’t be reassigned, as it communicates your intention clearly and helps prevent accidental reassignment of values. If a variable needs to be reassigned, use let
. Use var
only when dealing with older codebases or specific scenarios where its behavior is required (e.g., hoisting to the function scope).
console.log(x); // Output: undefined
var x = 10;
console.log(x); // Output: 10
Behind the scenes, the above code is interpreted as:
var x; // Declaration is hoisted
console.log(x); // Output: undefined
x = 10; // Assignment remains in place
console.log(x); // Output: 10
2. Function Hoisting: Function declarations are also hoisted to the top of their scope. This means that you can call a function before it appears in the code.
foo(); // Output: "Hello, I am foo."
function foo() {
console.log("Hello, I am foo.");
}
Behind the scenes, the above code is interpreted as:
function foo() {
console.log("Hello, I am foo.");
}
foo(); // Output: "Hello, I am foo."
It’s important to note that function expressions (functions assigned to variables) are not hoisted. Only function declarations are hoisted.
// Function expression (not hoisted)
var bar = function() {
console.log("I am a function expression.");
};
// Function declaration (hoisted)
function baz() {
console.log("I am a function declaration.");
}
While hoisting can be a useful feature, it can also lead to confusion and bugs if not understood properly. As a best practice, always declare your variables and functions at the top of their respective scopes to avoid unexpected behavior caused by hoisting. Additionally, consider using let
and const
instead of var
to avoid some of the issues associated with variable hoisting.
let x;
console.log(x); // Output: undefined
function foo(a) {
console.log(a);
}
foo(); // Output: undefined (a is not passed)
2. Null:null
is a special value that represents the intentional absence of any object value.
It is a type itself in JavaScript (
typeof null
returns"object"
– which is considered a historical mistake).If a variable is explicitly set to
null
, it means the variable has been assigned the value ofnull
, indicating that it points to no valid object or data.
let y = null;
console.log(y); // Output: null
Here’s a summary of the key differences:
undefined
is the default value of variables that have been declared but not assigned a value.null
is a value that represents the intentional absence of an object or data.Both
null
andundefined
are falsy values in JavaScript, meaning they evaluate tofalse
in boolean contexts.When checking for undefined, you can use
typeof
or strict equality (===
), but for null, it’s best to use strict equality (===
) becausetypeof null
returns"object"
.
console.log(typeof undefined); // Output: "undefined"
console.log(typeof null); // Output: "object"
let z;
console.log(z === undefined); // Output: true
console.log(z === null); // Output: false
In most cases, you will use null
to intentionally assign a variable with no value, while undefined
is typically the initial value of variables that have not been assigned. Understanding these differences is important when handling data and performing conditional checks in JavaScript.
function outerFunction() {
let outerVariable = "I am from the outer function";
function innerFunction() {
console.log(outerVariable); // Accessing the outer variable
}
return innerFunction; // Returning the inner function from the outer function
}
const closureFunc = outerFunction();
closureFunc(); // Output: "I am from the outer function"
In the example above, outerFunction
defines an inner function innerFunction
. The innerFunction
has access to the outerVariable
, which is a variable defined in the scope of outerFunction
. When outerFunction
is called and returns innerFunction
, the returned innerFunction
still holds a reference to the outerVariable
, creating a closure. Even though outerFunction
has finished executing, the closure ensures that innerFunction
can still access and use outerVariable
.
Closures are powerful because they allow functions to maintain state and create private variables. They are commonly used in scenarios like creating private variables, currying, and implementing the module pattern to encapsulate code.
However, it’s important to be cautious with closures to avoid potential memory leaks. If closures hold references to large objects or variables that are no longer needed, they can prevent those objects from being garbage-collected, leading to increased memory consumption. As long as closures are used wisely and mindfully, they provide a valuable feature in JavaScript for creating clean and modular code.




Popular Category
Topics for You
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36
Go through our study material. Your Job is awaiting.