JAVASCRIPT HANDWRITTEN NOTES

Javascript

JavaScript Handwritten Notes…

Contents:

1. Introduction to Javascript

2. Basics of JavaScript

3. Loops

4. Functions and scope

5. JavaScript statements

6. arrays and objects

7. DOM Manipulation

8. Asynchronous JavaScript

9. Error Handling

10. Es6 and beyond

11. Projects

12. Working with date and time

13. Ajax and fetch API

14. Introduction to Javascript framework and libraries..

 

How to download the PDF:

-The process is very simple.

 

-Tap on the Bottom link ๐Ÿ‘‡

 

-By tapping the link it will take you to a Telegram channel of the exact location.

 

-You can access it over from there

 

Download Here

 

1. What are the differences between var, let, and const in JavaScript?

Answer:

  • var: It has function scope, meaning the variable is accessible within the function it’s defined in. If it’s defined globally, it becomes a global variable. Variables declared using var can be re-assigned and re-declared.

  • let: It has block scope, meaning it’s only accessible within the block (e.g., within a for loop or a conditional). Variables declared using let can be re-assigned but not re-declared in the same scope.

  • const: It also has block scope but cannot be re-assigned once itโ€™s initialized. While the binding is constant, the value it points to can be mutable (for example, objects and arrays).


2. Explain the concept of closures in JavaScript.

Answer:

A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. Essentially, closures allow functions to “remember” their outer environment. This can be particularly useful for data encapsulation, creating private variables, or managing asynchronous operations.

Example:

javascript
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2

In this example, the inner function forms a closure, retaining access to count even after outer() has finished executing.


3. What is the difference between null and undefined?

Answer:

  • undefined: It is a primitive value that is automatically assigned to variables that have been declared but not yet assigned a value. It is also the default return value of a function that does not explicitly return anything.

  • null: It is an assignment value, used to represent the intentional absence of any object value. It is an object type, but it is used to signify that a variable should be empty.


4. What are JavaScript Promises, and how do they work?

Answer:

A Promise is an object representing the eventual completion or failure of an asynchronous operation. It has three states:

  • Pending: The promise is still in progress.
  • Resolved (Fulfilled): The promise has been successfully completed.
  • Rejected: The promise has failed.

You can use .then() to handle the resolved value and .catch() to handle the rejection.

Example:

javascript
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve(“Success!”);
} else {
reject(“Failure!”);
}
});

promise.then(result => console.log(result)).catch(error => console.log(error));


5. What is event delegation in JavaScript?

Answer:

Event delegation is a technique where you attach a single event listener to a parent element instead of multiple event listeners on individual child elements. This improves performance and reduces memory usage.

The event listener on the parent element checks the event’s target and delegates the action to the appropriate child.

Example:

javascript
document.getElementById("parent").addEventListener("click", function(event) {
if (event.target && event.target.matches("button.classname")) {
console.log("Button clicked!");
}
});

6. What is the purpose of this keyword in JavaScript?

Answer:

The this keyword in JavaScript refers to the context in which a function is executed. Its value is determined by how a function is called:

  • In a global context, this refers to the global object (window in browsers).
  • In an object method, this refers to the object the method is called on.
  • In a constructor function, this refers to the new instance of the object being created.
  • With arrow functions, this is lexically bound, meaning it inherits this from its surrounding context.

Example:

javascript
const person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name); // 'this' refers to the person object
}
};
person.greet(); // “Hello, Alice”


7. Explain the concept of asynchronous programming and callbacks in JavaScript.

Answer:

Asynchronous programming allows a program to execute non-blocking operations, such as network requests or file reads, without freezing the main thread. This enables the program to remain responsive while waiting for these operations to complete.

Callbacks are functions passed as arguments to other functions and executed once the task is complete. However, callbacks can result in “callback hell,” where nested callbacks lead to difficult-to-manage code.

Example of a callback:

javascript
function fetchData(callback) {
setTimeout(() => {
callback("Data fetched");
}, 1000);
}
fetchData((message) => {
console.log(message); // “Data fetched”
});


8. What is the difference between == and === in JavaScript?

Answer:

  • == (Loose equality): It compares values after converting them to the same type (type coercion). For example, 5 == "5" returns true.

  • === (Strict equality): It compares both the value and the type without performing type coercion. For example, 5 === "5" returns false.


9. What is hoisting in JavaScript?

Answer:

Hoisting is JavaScript’s behavior of moving variable and function declarations to the top of their containing scope before code execution. However, only the declarations are hoisted, not the initializations.

  • Function declarations are hoisted completely, meaning they are available throughout the scope.
  • Variable declarations (with var) are hoisted, but their initializations are not.

Example:

javascript
console.log(a); // undefined
var a = 10;

10. What are higher-order functions in JavaScript?

Answer:

A higher-order function is a function that either accepts one or more functions as arguments or returns a function as its result. Common examples of higher-order functions are map(), filter(), and reduce().

Example:

javascript
function multiplyByTwo(x) {
return x * 2;
}
function processArray(arr, operation) {
return arr.map(operation);
}

const numbers = [1, 2, 3];
console.log(processArray(numbers, multiplyByTwo)); // [2, 4, 6]


These are foundational JavaScript questions and answers that will help you understand key concepts and prepare for technical interviews.

Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *