Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b951bb

Browse files
Added Question-Answer 22-29
1 parent 3d78fd5 commit 7b951bb

File tree

1 file changed

+150
-3
lines changed

1 file changed

+150
-3
lines changed

‎README.md

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ Top JavaScript interview questions
2727
| 19 | [What is a callback function](#19-what-is-a-callback-function) |
2828
| 20 | [Is it possible to have both local and global variables with the same name](#20-is-it-possible-to-have-both-local-and-global-variables-with-the-same-name) |
2929
| 21 | [Difference between Local Storage and Session Storage](#21-difference-between-local-storage-and-session-storage) |
30+
| 22 | [Difference between forEach() and map()](#22-difference-between-foreach-and-map) |
31+
| 23 | [What is rest operator](#23-what-is-rest-operator) |
32+
| 24 | [What is spread operator](#24-what-is-spread-operator) |
33+
| 25 | [Difference between async and defer](#25-difference-between-async-and-defer) |
34+
| 26 | [What is Nullish coalescing operator](#26-what-is-nullish-coalescing-operator) |
35+
| 27 | [What is the difference between a parameter and an argument](#27-what-is-the-difference-between-a-parameter-and-an-argument) |
36+
| 28 | [What is a closure](#28-what-is-a-closure) |
37+
| 29 | [Difference between function declaration and function expression](#29-difference-between-function-declaration-and-function-expression) |
38+
3039

3140
### 1. What is JavaScript
3241
* JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
@@ -84,7 +93,13 @@ console.log(person.name); //output - Surbhi
8493

8594
### 4. Difference between "==" and "===" operators
8695
* == : While comparing two operands, checks for only value
96+
```js
97+
console.log(1=="1"); // output=>>>>>>>>> true
98+
```
8799
* === : While comparing two operands, checks for value as well as data type
100+
```js
101+
console.log(1==="1"); // output=>>>>>>>>> false
102+
```
88103

89104
### 5. Is javascript single-threaded or multi-threaded
90105
* JavaScript is Single-threaded
@@ -200,7 +215,7 @@ The output of above code will be 10 because the variable "a" is hoisted to the t
200215
```javascript
201216
demo(); // demo console
202217
function demo() {
203-
console.log('demo console'); //
218+
console.log('demo console');
204219
}
205220
```
206221
The output of above code will be "demo console" because the function declaration is hoisted to the top of the scope, allowing it to be called before it is declared in the code.
@@ -247,8 +262,8 @@ function memoizedAdd(num1, num2) {
247262
}
248263

249264
const add = memoizedAdd();
250-
console.log(add(2, 3)); // "Calculating result:", "2 and 3", output=====> 5
251-
console.log(add(2, 3)); // "Retrieving from cache:", "2 and 3" output=====> 5
265+
console.log(add(2, 3)); // "Calculating result:", "2 and 3", output ========> 5
266+
console.log(add(2, 3)); // "Retrieving from cache:", "2 and 3" output ========> 5
252267
```
253268

254269
### 18. Difference between var, let and const
@@ -298,6 +313,138 @@ Local storage and session storage are web storage provided by web browsers to st
298313

299314
**Session Storage** - Data stored in session storage will be deleted when the browser window is closed or the session ends and it is available only within the same window or tab that created the data.
300315

316+
### 22. Difference between forEach() and map()
317+
map() and forEach() are array methods that can be used to iterate over an array.
318+
**map()**
319+
1. map() method receives a function as an argument, executes it once for each array element and returns a new array
320+
2. It is generally used when we need to modify/change data, because it returns a new array with the transformed data
321+
3. It is chainable because we can attach sort(), filter() etc. after performing a map() method on an array
322+
323+
```js
324+
const arr = [1,2,3,4,5]
325+
let result = arr.map(x => x * x)
326+
console.log(result) // output ========> [1,4,9,16,25]
327+
```
328+
329+
**forEach()**
330+
1. forEach() method receives a function as an argument, executes it once for each array element but returns undefined.
331+
2. It is generally used when we just need to iterate over an array
332+
3. It is not chainable
333+
334+
```js
335+
const arr = [1,2,3,4,5]
336+
let result = arr.forEach(x => x * x)
337+
console.log(result) // output ========> undefined
338+
```
339+
340+
### 23. What is rest operator
341+
1. The rest operator was introduced in ES6 (ECMAScript 2015) and it is represented by three dots (...)
342+
2. It is used in function parameters and allows you to represent an indefinite number of arguments as an array
343+
344+
```js
345+
function addition(...numbers) {
346+
return numbers.reduce((a,b)=>a+b);
347+
}
348+
console.log(addition(1, 2, 3, 4)); // output ========> 10
349+
```
350+
351+
3. It collects all the remaining arguments passed to a function into an array
352+
353+
```js
354+
function profile(name, designation,...location) {
355+
return location
356+
}
357+
console.log(profile("surbhi","SE","India","Indore","M.P")); // output ========> ["India", "Indore", "M.P"]
358+
```
359+
360+
### 24. What is spread operator
361+
1. The spread operator was introduced in ES6 (ECMAScript 2015) and it is represented by three dots (...)
362+
2. It allows an iterable to be spread into individual elements
363+
```js
364+
const arr = [1, 2, 3, 4,5];
365+
console.log(...arr); // output ======> 1, 2, 3, 4, 5
366+
```
367+
3. It can be used to copy the elements of an existing array into a new array, or to concatenate arrays
368+
```js
369+
const arr1 = [1, 2, 3];
370+
const arr2 = [4, 5, 6];
371+
const arr3 = [...arr1, ...arr2];
372+
console.log(arr3); // output =======> [1, 2, 3, 4, 5, 6]
373+
```
374+
4. It can be used to copy the properties of an existing object into a new object
375+
```js
376+
const obj1 = { a:1, b:2 };
377+
const obj2 = { c:3 };
378+
const obj3 = { ...obj1, ...obj2 };
379+
console.log(obj3); // output ========> { a: 1, b: 2, c: 3 }
380+
```
381+
382+
### 25. Difference between async and defer
383+
async and defer are the attributes used with the script tag in HTML to load javaScript files.
384+
385+
**Async** - The script is loaded in parallel to the HTML parsing, and executed as soon as it is available i.e., it executes the script immediately after it is loaded. It can be useful for scripts that don't depend on the DOM.
386+
387+
**Defer** - The script is loaded in parallel to the HTML parsing, and executed after the page is completely loaded i.e., it waits until the page has finished parsing. It can be useful for scripts that depend on the DOM.
388+
389+
### 26. What is Nullish coalescing operator
390+
1. The nullish coalescing (??) operator is a logical operator
391+
2. It allows you to check if a value is either null or undefined
392+
3. It returns its right-hand side operand if its left-hand side operand is null or undefined, otherwise returns its left-hand side operand
393+
```js
394+
console.log((null || undefined) ?? "foo"); // output ========> "foo"
395+
console.log("hello" ?? "foo"); // output ========> "hello"
396+
```
397+
398+
### 27. What is the difference between a parameter and an argument
399+
**Parameter** - A parameter is a variable that is defined during a function declaration or definition. It represents a value that the function or method expects to receive as an input.
400+
401+
**Argument** - An argument is the actual value that is passed to a function or method when it is called.
402+
```js
403+
function Demo(parameter1, parameter2){
404+
something------
405+
something------
406+
}
407+
Demo(argument1, argument2);
408+
```
409+
410+
### 28. What is a closure
411+
A closure is a combination of a function and the environment in which it was created(lexical scope). It gives you access to an outer function's scope from an inner function even if the outer function has returned
412+
```js
413+
function Demo() {
414+
let name = "Surbhi";
415+
function displayName() {
416+
console.log(name);
417+
}
418+
return displayName;
419+
}
420+
421+
const result = Demo();
422+
result();
423+
```
424+
425+
### 29. Difference between function declaration and function expression
426+
**Function Declaration**
427+
1. A function declaration defines a function using the function keyword, followed by the function name
428+
2. We can call a function, declared using a function declaration, before it is defined. Because it is hoisted to the top of its scope
429+
3. It does not require a variable assignment
430+
```js
431+
function Message() {
432+
console.log("Welcome Message"); // output ========> Welcome Message
433+
}
434+
Message();
435+
```
436+
437+
**Function Expression**
438+
1. A function Expression is similar to a function declaration without the function name
439+
2. We can not call a function, declared using a function expression, before it is defined
440+
3. It can be stored in a variable assignment
441+
```js
442+
const Message = function() {
443+
console.log("Welcome Message"); // output ========> Welcome Message
444+
}
445+
Message();
446+
```
447+
301448
******************************In progress
302449
303450

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /