JS-5
JS-5
Explain each
one of them in detail?
1. Arrow Functions:
o Syntax for writing concise function expressions. They use the => syntax.
o Example:
javascript
Copy code
const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5
javascript
Copy code
const name = 'John';
const greeting = `Hello, ${name}!`;
console.log(greeting); // Output: Hello, John!
3. Default Parameters:
o Function parameters can have default values if no argument is provided.
o Example:
javascript
Copy code
function greet(name = 'Guest') {
console.log(`Hello, ${name}!`);
}
greet(); // Output: Hello, Guest!
greet('John'); // Output: Hello, John!
4. Destructuring Assignment:
o A syntax for unpacking values from arrays or properties from objects into
distinct variables.
o Example:
javascript
Copy code
const [a, b] = [1, 2];
console.log(a, b); // Output: 1 2
javascript
Copy code
// Spread
const arr = [1, 2, 3];
const newArr = [...arr, 4, 5];
console.log(newArr); // Output: [1, 2, 3, 4, 5]
// Rest
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3)); // Output: 6
6. Classes:
o Syntactic sugar for constructor functions and prototypes-based inheritance.
o Example:
javascript
Copy code
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
javascript
Copy code
const name = 'John';
const age = 30;
const person = {
name,
age,
greet() {
console.log(`Hello, my name is ${this.name}`);
}
};
8. Modules:
o Native support for modular code using import and export statements.
o Example:
javascript
Copy code
// In module.js
export const greet = name => `Hello, ${name}`;
// In main.js
import { greet } from './module.js';
console.log(greet('John')); // Output: Hello, John
9. Promises:
o Objects representing the eventual completion or failure of an asynchronous
operation.
o Example:
javascript
Copy code
const fetchData = () => new Promise((resolve, reject) => {
setTimeout(() => resolve('Data received'), 1000);
});
10. Generators:
o Functions that can be paused and resumed, producing a sequence of values.
o Example:
javascript
Copy code
function* generator() {
yield 1;
yield 2;
yield 3;
}
Question 2: Call, Apply, Bind. When to use what? Can you give an example?
• Call:
o Method to invoke a function with a specified this value and arguments
provided individually.
o Example:
javascript
Copy code
function greet() {
console.log(`Hello, ${this.name}`);
}
• Apply:
o Method to invoke a function with a specified this value and arguments
provided as an array.
o Example:
javascript
Copy code
function greet(greeting, punctuation) {
console.log(`${greeting}, ${this.name}${punctuation}`);
}
• Bind:
o Method to create a new function with a specified this value and initial
arguments.
o Example:
javascript
Copy code
function greet(greeting, punctuation) {
console.log(`${greeting}, ${this.name}${punctuation}`);
}
Question 3: What are different ways to store data in the browser? Explain each one of
them and when should we use what?
• Cookies:
o Small pieces of data stored by the browser, sent with every HTTP request.
o Use for: Storing small amounts of data that needs to be sent to the server with
every request, like session identifiers.
• Local Storage:
o Provides a way to store key-value pairs in the browser with no expiration date.
o Use for: Storing data that needs to persist across sessions, like user
preferences.
o Example:
javascript
Copy code
localStorage.setItem('key', 'value');
console.log(localStorage.getItem('key')); // Output: value
• Session Storage:
o Similar to local storage but data is cleared when the page session ends.
o Use for: Storing data that only needs to persist for a single session.
o Example:
javascript
Copy code
sessionStorage.setItem('key', 'value');
console.log(sessionStorage.getItem('key')); // Output: value
• IndexedDB:
o A low-level API for storing large amounts of structured data.
o Use for: Complex data storage requirements, like offline applications.
o Example:
javascript
Copy code
const request = indexedDB.open('myDatabase', 1);
request.onsuccess = function(event) {
const db = event.target.result;
console.log('Database opened successfully');
};
Question 4: What are generator functions in JavaScript? How are they different from
normal functions?
• Generator Functions:
o Functions that can be paused and resumed, producing a sequence of values
using the function* syntax.
o Example:
javascript
Copy code
function* generator() {
yield 1;
yield 2;
yield 3;
}
• array.forEach:
o Iterates over an array and executes a provided function once for each array
element. Does not return a new array.
o Example:
javascript
Copy code
const arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2));
// Output: 2 4 6
• array.map:
o Creates a new array with the results of calling a provided function on every
element in the array.
o Example:
javascript
Copy code
const arr = [1, 2, 3];
const newArr = arr.map(num => num * 2);
console.log(newArr); // Output: [2, 4, 6]
• array.filter:
o Creates a new array with all elements that pass the test implemented by the
provided function.
o Example:
javascript
Copy code
const arr = [1, 2, 3, 4];
const newArr = arr.filter(num => num % 2 === 0);
console.log(newArr); // Output: [2, 4]
• array.reduce:
o Executes a reducer function on each element of the array, resulting in a single
output value.
o Example:
javascript
Copy code
const arr = [1, 2, 3, 4];
const sum = arr.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 10
o The reducer function takes four arguments: accumulator, current value, current
index, and the array being processed.
o Useful for summing values, flattening arrays, or creating more complex data
structures.
Feel free to ask for more details or examples on any specific feature or concept!