|
18 | 18 |
|
19 | 19 | <div>
|
20 | 20 | <p align="center">
|
21 |
| - <a href="https://www.frontendlead.com/coding-questions?utm_source=github&utm_medium=referral&ut%0Dm_campaign=sudheerj-js"> |
| 21 | + <a href="https://www.frontendlead.com/coding-questions?utm_source=github&utm_medium=referral&ut%0Dm_campaign=sudheerj-js" rel="dofollow"> |
22 | 22 | <img src="./images/collab/frontendlead-banner.png" alt="FrontEndLead JavaScript Interview Questions" width="100%">
|
23 | 23 | </a>
|
24 | 24 | </p>
|
|
2428 | 2428 |
|
2429 | 2429 | ```javascript
|
2430 | 2430 | <script>
|
2431 |
| - var msg; |
2432 |
| - function greeting() { |
2433 |
| - alert('Good morning'); |
2434 |
| - } |
2435 |
| - function start() { |
2436 |
| - msg =setTimeout(greeting, 3000); |
| 2431 | + var msg; |
| 2432 | + function greeting() { |
| 2433 | + alert('Good morning'); |
| 2434 | + } |
| 2435 | + function start() { |
| 2436 | + msg =setTimeout(greeting, 3000); |
2437 | 2437 |
|
2438 |
| - } |
| 2438 | + } |
2439 | 2439 |
|
2440 |
| - function stop() { |
2441 |
| - clearTimeout(msg); |
2442 |
| - } |
| 2440 | + function stop() { |
| 2441 | + clearTimeout(msg); |
| 2442 | + } |
2443 | 2443 | </script>
|
2444 | 2444 | ```
|
2445 | 2445 |
|
|
2453 | 2453 |
|
2454 | 2454 | ```javascript
|
2455 | 2455 | <script>
|
2456 |
| - var msg; |
2457 |
| - function greeting() { |
2458 |
| - alert('Good morning'); |
2459 |
| - } |
2460 |
| - function start() { |
2461 |
| - msg = setInterval(greeting, 3000); |
| 2456 | + var msg; |
| 2457 | + function greeting() { |
| 2458 | + alert('Good morning'); |
| 2459 | + } |
| 2460 | + function start() { |
| 2461 | + msg = setInterval(greeting, 3000); |
2462 | 2462 |
|
2463 |
| - } |
| 2463 | + } |
2464 | 2464 |
|
2465 |
| - function stop() { |
2466 |
| - clearInterval(msg); |
2467 |
| - } |
| 2465 | + function stop() { |
| 2466 | + clearInterval(msg); |
| 2467 | + } |
2468 | 2468 | </script>
|
2469 | 2469 | ```
|
2470 | 2470 |
|
|
2494 | 2494 | mainString.includes(subString);
|
2495 | 2495 | ```
|
2496 | 2496 |
|
2497 |
| - 1. **Using indexOf:** In an ES5 or older environment, you can use `String.prototype.indexOf` which returns the index of a substring. If the index value is not equal to -1 then it means the substring exists in the main string. |
| 2497 | + 2. **Using indexOf:** In an ES5 or older environment, you can use `String.prototype.indexOf` which returns the index of a substring. If the index value is not equal to -1 then it means the substring exists in the main string. |
2498 | 2498 |
|
2499 | 2499 | ```javascript
|
2500 | 2500 | var mainString = "hello",
|
2501 | 2501 | subString = "hell";
|
2502 | 2502 | mainString.indexOf(subString) !== -1;
|
2503 | 2503 | ```
|
2504 | 2504 |
|
2505 |
| - 1. **Using RegEx:** The advanced solution is using Regular expression's test method(`RegExp.test`), which allows for testing for against regular expressions |
| 2505 | + 3. **Using RegEx:** The advanced solution is using Regular expression's test method(`RegExp.test`), which allows for testing for against regular expressions |
2506 | 2506 |
|
2507 | 2507 | ```javascript
|
2508 | 2508 | var mainString = "hello",
|
|
2570 | 2570 |
|
2571 | 2571 | 1. **Using in operator:** You can use the in operator whether a key exists in an object or not
|
2572 | 2572 |
|
2573 |
| - ```javascript |
2574 |
| - "key" in obj; |
2575 |
| - ``` |
| 2573 | + ```javascript |
| 2574 | + "key" in obj; |
| 2575 | + ``` |
2576 | 2576 |
|
2577 |
| - and If you want to check if a key doesn't exist, remember to use parenthesis, |
| 2577 | + and If you want to check if a key doesn't exist, remember to use parenthesis, |
2578 | 2578 |
|
2579 |
| - ```javascript |
2580 |
| - !("key" in obj); |
2581 |
| - ``` |
| 2579 | + ```javascript |
| 2580 | + !("key" in obj); |
| 2581 | + ``` |
2582 | 2582 |
|
2583 |
| - 1. **Using hasOwnProperty method:** You can use `hasOwnProperty` to particularly test for properties of the object instance (and not inherited properties) |
| 2583 | + 2. **Using hasOwnProperty method:** You can use `hasOwnProperty` to particularly test for properties of the object instance (and not inherited properties) |
2584 | 2584 |
|
2585 |
| - ```javascript |
2586 |
| - obj.hasOwnProperty("key"); // true |
2587 |
| - ``` |
| 2585 | + ```javascript |
| 2586 | + obj.hasOwnProperty("key"); // true |
| 2587 | + ``` |
2588 | 2588 |
|
2589 |
| - 1. **Using undefined comparison:** If you access a non-existing property from an object, the result is undefined. Let’s compare the properties against undefined to determine the existence of the property. |
| 2589 | + 3. **Using undefined comparison:** If you access a non-existing property from an object, the result is undefined. Let’s compare the properties against undefined to determine the existence of the property. |
2590 | 2590 |
|
2591 |
| - ```javascript |
2592 |
| - const user = { |
2593 |
| - name: "John", |
2594 |
| - }; |
| 2591 | + ```javascript |
| 2592 | + const user = { |
| 2593 | + name: "John", |
| 2594 | + }; |
2595 | 2595 |
|
2596 |
| - console.log(user.name !== undefined); // true |
2597 |
| - console.log(user.nickName !== undefined); // false |
2598 |
| - ``` |
| 2596 | + console.log(user.name !== undefined); // true |
| 2597 | + console.log(user.nickName !== undefined); // false |
| 2598 | + ``` |
2599 | 2599 |
|
2600 | 2600 | **[⬆ Back to Top](#table-of-contents)**
|
2601 | 2601 |
|
|
2698 | 2698 |
|
2699 | 2699 | #### Cons
|
2700 | 2700 |
|
2701 |
| - 1. Too verbose |
2702 |
| - 2. Imperative |
2703 |
| - 3. You might face off-by-one errors. |
| 2701 | + 3. Too verbose |
| 2702 | + 4. Imperative |
| 2703 | + 5. You might face off-by-one errors. |
2704 | 2704 |
|
2705 | 2705 | **[⬆ Back to Top](#table-of-contents)**
|
2706 | 2706 |
|
|
2770 | 2770 |
|
2771 | 2771 | 135. ### How do you add a key value pair in javascript
|
2772 | 2772 |
|
2773 |
| - There are two possible solutions to add new properties to an object. Let's take a simple object to explain these solutions. |
| 2773 | + There are two possible solutions to add new properties to an object. |
| 2774 | + |
| 2775 | + Let's take a simple object to explain these solutions. |
2774 | 2776 |
|
2775 | 2777 | ```javascript
|
2776 | 2778 | var object = {
|
|
4490 | 4492 |
|
4491 | 4493 | The above code processed in a call stack as below,
|
4492 | 4494 |
|
4493 |
| - 1. Add the `hungry()` function to the call stack list and execute the code. |
4494 |
| - 2. Add the `eatFruits()` function to the call stack list and execute the code. |
4495 |
| - 3. Delete the `eatFruits()` function from our call stack list. |
4496 |
| - 4. Delete the `hungry()` function from the call stack list since there are no items anymore. |
| 4495 | + 3. Add the `hungry()` function to the call stack list and execute the code. |
| 4496 | + 4. Add the `eatFruits()` function to the call stack list and execute the code. |
| 4497 | + 5. Delete the `eatFruits()` function from our call stack list. |
| 4498 | + 6. Delete the `hungry()` function from the call stack list since there are no items anymore. |
4497 | 4499 |
|
4498 | 4500 | 
|
4499 | 4501 |
|
|
4576 | 4578 |
|
4577 | 4579 | 243. ### What is the purpose of compareFunction while sorting arrays
|
4578 | 4580 |
|
4579 |
| - The compareFunction is used to define the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value. Let's take an example to see the usage of compareFunction, |
| 4581 | + The compareFunction is used to define the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value. |
| 4582 | + |
| 4583 | + Let's take an example to see the usage of compareFunction, |
4580 | 4584 |
|
4581 | 4585 | ```javascript
|
4582 | 4586 | let numbers = [1, 2, 5, 3, 4];
|
|
5370 | 5374 |
|
5371 | 5375 | 1. **Using Object Literal Notation:** Let's wrap variables and functions inside an Object literal which acts as a namespace. After that you can access them using object notation
|
5372 | 5376 |
|
5373 |
| - ```javascript |
5374 |
| - var namespaceOne = { |
5375 |
| - function func1() { |
5376 |
| - console.log("This is a first definition"); |
| 5377 | + ```javascript |
| 5378 | + var namespaceOne = { |
| 5379 | + function func1() { |
| 5380 | + console.log("This is a first definition"); |
| 5381 | + } |
5377 | 5382 | }
|
5378 |
| - } |
5379 |
| - var namespaceTwo = { |
5380 |
| - function func1() { |
5381 |
| - console.log("This is a second definition"); |
| 5383 | + var namespaceTwo = { |
| 5384 | + function func1() { |
| 5385 | + console.log("This is a second definition"); |
| 5386 | + } |
5382 | 5387 | }
|
5383 |
| - } |
5384 |
| - namespaceOne.func1(); // This is a first definition |
5385 |
| - namespaceTwo.func1(); // This is a second definition |
5386 |
| - ``` |
| 5388 | + namespaceOne.func1(); // This is a first definition |
| 5389 | + namespaceTwo.func1(); // This is a second definition |
| 5390 | + ``` |
5387 | 5391 |
|
5388 | 5392 | 2. **Using IIFE (Immediately invoked function expression):** The outer pair of parentheses of IIFE creates a local scope for all the code inside of it and makes the anonymous function a function expression. Due to that, you can create the same function in two different function expressions to act as a namespace.
|
5389 | 5393 |
|
5390 |
| - ```javascript |
5391 |
| - (function () { |
5392 |
| - function fun1() { |
5393 |
| - console.log("This is a first definition"); |
5394 |
| - } |
5395 |
| - fun1(); |
5396 |
| - })(); |
| 5394 | + ```javascript |
| 5395 | + (function () { |
| 5396 | + function fun1() { |
| 5397 | + console.log("This is a first definition"); |
| 5398 | + } |
| 5399 | + fun1(); |
| 5400 | + })(); |
5397 | 5401 |
|
5398 |
| - (function () { |
5399 |
| - function fun1() { |
5400 |
| - console.log("This is a second definition"); |
5401 |
| - } |
5402 |
| - fun1(); |
5403 |
| - })(); |
5404 |
| - ``` |
| 5402 | + (function () { |
| 5403 | + function fun1() { |
| 5404 | + console.log("This is a second definition"); |
| 5405 | + } |
| 5406 | + fun1(); |
| 5407 | + })(); |
| 5408 | + ``` |
5405 | 5409 |
|
5406 | 5410 | 3. **Using a block and a let/const declaration:** In ECMAScript 6, you can simply use a block and a let declaration to restrict the scope of a variable to a block.
|
5407 | 5411 |
|
5408 |
| - ```javascript |
5409 |
| - { |
5410 |
| - let myFunction = function fun1() { |
5411 |
| - console.log("This is a first definition"); |
5412 |
| - }; |
5413 |
| - myFunction(); |
5414 |
| - } |
5415 |
| - //myFunction(): ReferenceError: myFunction is not defined. |
| 5412 | + ```javascript |
| 5413 | + { |
| 5414 | + let myFunction = function fun1() { |
| 5415 | + console.log("This is a first definition"); |
| 5416 | + }; |
| 5417 | + myFunction(); |
| 5418 | + } |
| 5419 | + //myFunction(): ReferenceError: myFunction is not defined. |
5416 | 5420 |
|
5417 |
| - { |
5418 |
| - let myFunction = function fun1() { |
5419 |
| - console.log("This is a second definition"); |
5420 |
| - }; |
5421 |
| - myFunction(); |
5422 |
| - } |
5423 |
| - //myFunction(): ReferenceError: myFunction is not defined. |
5424 |
| - ``` |
| 5421 | + { |
| 5422 | + let myFunction = function fun1() { |
| 5423 | + console.log("This is a second definition"); |
| 5424 | + }; |
| 5425 | + myFunction(); |
| 5426 | + } |
| 5427 | + //myFunction(): ReferenceError: myFunction is not defined. |
| 5428 | + ``` |
5425 | 5429 |
|
5426 | 5430 | **[⬆ Back to Top](#table-of-contents)**
|
5427 | 5431 |
|
|
5946 | 5950 |
|
5947 | 5951 | 1. Import a module on-demand or conditionally. For example, if you want to load a polyfill on legacy browser
|
5948 | 5952 |
|
5949 |
| - ```javascript |
5950 |
| - if (isLegacyBrowser()) { |
5951 |
| - import(···) |
5952 |
| - .then(···); |
5953 |
| - } |
5954 |
| - ``` |
| 5953 | + ```javascript |
| 5954 | + if (isLegacyBrowser()) { |
| 5955 | + import(···) |
| 5956 | + .then(···); |
| 5957 | + } |
| 5958 | + ``` |
5955 | 5959 |
|
5956 |
| - 1. Compute the module specifier at runtime. For example, you can use it for internationalization. |
| 5960 | + 2. Compute the module specifier at runtime. For example, you can use it for internationalization. |
5957 | 5961 |
|
5958 |
| - ```javascript |
5959 |
| - import(`messages_${getLocale()}.js`).then(···); |
5960 |
| - ``` |
| 5962 | + ```javascript |
| 5963 | + import(`messages_${getLocale()}.js`).then(···); |
| 5964 | + ``` |
5961 | 5965 |
|
5962 |
| - 1. Import a module from within a regular script instead a module. |
| 5966 | + 3. Import a module from within a regular script instead a module. |
5963 | 5967 |
|
5964 | 5968 | **[⬆ Back to Top](#table-of-contents)**
|
5965 | 5969 |
|
|
6017 | 6021 | console.log(l10nSV.compare("ä", "z") === +1); // true
|
6018 | 6022 | ```
|
6019 | 6023 |
|
6020 |
| - 1. **Sorting:** |
| 6024 | + 2. **Sorting:** |
6021 | 6025 |
|
6022 | 6026 | ```javascript
|
6023 | 6027 | var list = ["ä", "a", "z"]; // In German, "ä" sorts with "a" Whereas in Swedish, "ä" sorts after "z"
|
|
6236 | 6240 | var n = 022;
|
6237 | 6241 | ```
|
6238 | 6242 |
|
6239 |
| - 1. Using `with` statement |
6240 |
| - 2. When you use delete operator on a variable name |
6241 |
| - 3. Using eval or arguments as variable or function argument name |
6242 |
| - 4. When you use newly reserved keywords |
6243 |
| - 5. When you declare a function in a block and access it from outside of the block |
| 6243 | + 2. Using `with` statement |
| 6244 | + 3. When you use delete operator on a variable name |
| 6245 | + 4. Using eval or arguments as variable or function argument name |
| 6246 | + 5. When you use newly reserved keywords |
| 6247 | + 6. When you declare a function in a block and access it from outside of the block |
6244 | 6248 |
|
6245 | 6249 | ```javascript
|
6246 | 6250 | if (someCondition) {
|
|
7021 | 7025 |
|
7022 | 7026 | In this API, browser is going to ask you for permission to use your microphone
|
7023 | 7027 |
|
7024 |
| - 1. **SpeechSynthesis (Text-to-Speech):** It provides the ability to recognize voice context from an audio input and respond. This is accessed by the `SpeechSynthesis` interface. |
| 7028 | + 2. **SpeechSynthesis (Text-to-Speech):** It provides the ability to recognize voice context from an audio input and respond. This is accessed by the `SpeechSynthesis` interface. |
7025 | 7029 | For example, the below code is used to get voice/speech from text,
|
7026 | 7030 |
|
7027 | 7031 | ```javascript
|
@@ -8840,10 +8844,12 @@ The execution context is created when a function is called. The function's code
|
8840 | 8844 | **[⬆ Back to Top](#table-of-contents)**
|
8841 | 8845 |
|
8842 | 8846 | 467. ### What is the difference between substring and substr methods?
|
| 8847 | + Both substring() and substr() are string methods, which are used to find substring of a given string. But there are some notable differences with their usage, |
| 8848 | + |
8843 | 8849 |
|
8844 | 8850 | **[⬆ Back to Top](#table-of-contents)**
|
8845 | 8851 |
|
8846 |
| -468. ### How to find the number of parameters expected by a function? |
| 8852 | +1. ### How to find the number of parameters expected by a function? |
8847 | 8853 | The function's object has a **length** property which tells you how many formal parameters expected by a function. This is a static value defined by the function, not the number of arguments the function is called with(__arguments.length__). The basic usage of length propery is,
|
8848 | 8854 |
|
8849 | 8855 | ```javascript
|
|
0 commit comments