Posts

Showing posts with the label Javascript

Variables, Data Types, and Operators in JavaScript

Image
Variables 1. Declaration var Historically, variables were declared using the var keyword. However, it has some scoping issues and is generally replaced by let and const in modern JavaScript. javascript var x = 10; let Introduced in ES6, let allows you to declare block-scoped variables. This means the variable is only available within the block it is defined. javascript let y = 20; const Also introduced in ES6, const is used to declare block-scoped variables that are read-only. Once a value is assigned to a const variable, it cannot be reassigned. javascript const z = 30; 2. Variable Scope Global Scope: Variables declared outside of any function or block are in the global scope and can be accessed from anywhere in the code. Function Scope: Variables declared within a function using var are confined to the function scope. Block Scope: Variables declared with let or const within a block (denoted by {}) are confined to that block. javascript if (true) {   var a = 40;  // global scope (if

Javascript interviews questions and answers with code

Image
JavaScript interview questions along with answers and code examples 1. Question: What is the difference between null and undefined in JavaScript? Answer: null is a deliberate assignment representing the absence of any object value. undefined is a variable that has been declared but not assigned any value. 2. Question: Explain the concept of closures in JavaScript. Answer: Closures occur when a function is defined inside another function, allowing the inner function to access the outer function's variables. This creates a scope chain. function outerFunction() { let outerVariable = 'I am outer!'; function innerFunction() { console.log(outerVariable); } return innerFunction; } let closureExample = outerFunction(); closureExample(); // Outputs: I am outer! 3. Question: What is the event loop in JavaScript? Answer: The event loop is a mechanism that handles asynchronous tasks in JavaScript. It continuously checks the call stack and the callback queue. When th