Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gowthamand/javascript-advanced-es6-interview-question
Javascript Advanced ES6-Interview Question
https://github.com/gowthamand/javascript-advanced-es6-interview-question
angular core-concepts es6 es6-javascript interview javascript javascript-advanced logical-operators
Last synced: 17 days ago
JSON representation
Javascript Advanced ES6-Interview Question
- Host: GitHub
- URL: https://github.com/gowthamand/javascript-advanced-es6-interview-question
- Owner: gowthamand
- Created: 2020-03-01T03:14:49.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-11T07:30:37.000Z (almost 5 years ago)
- Last Synced: 2024-11-17T08:29:19.882Z (2 months ago)
- Topics: angular, core-concepts, es6, es6-javascript, interview, javascript, javascript-advanced, logical-operators
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript ES6 Interview Questions & Answers
### Table of Contents
| No. | Questions |
|---- | ---------
|1 | [What is IIFE's (Immediately-Invoked Function Expression)](#what-is-iifes)|
|2 | [What's the difference between .call and .apply?](#whats-the-difference-between-call-and-apply)|
|2 | [What's the difference between using “let” and “var” to declare a variable in ES6?](#whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-es6)|
1. ### What is IIFE's
It’s an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it’s created.
It has nothing to do with any event-handler for any events (such as document.onload).
Consider the part within the first pair of parentheses: ```(function(){})();```....it is a regular function expression. Then look at the last pair ``` (function(){})(); ```, this is normally added to an expression to call a function; in this case, our prior expression.
This pattern is often used when trying to avoid polluting the global namespace, because all the variables used inside the IIFE (like in any other normal function) are not visible outside its scope.
This is why, maybe, you confused this construction with an event-handler for window.onload, because it’s often used as this
```typescript
(function(){
var foo = function() {};
window.onload = foo;
})();
(function IIFE(){
console.log( "Hello!" );
})();
```
**[⬆ Back to Top](#table-of-contents)**2. ### What's the difference between .call and .apply?
Both .call and .apply are used to invoke functions and the first parameter will be used as the value of this within the function. However, .call takes in comma-separated arguments as the next arguments while .apply takes in an array of arguments as the next argument. An easy way to remember this is C for call and comma-separated and A for apply and an array of arguments.
```typescript
function add(a, b){
return a + b;
}
console.log(add.call(null, 1, 2)); // 3
console.log(add.apply(null, [1, 2])); // 3
```
**[⬆ Back to Top](#table-of-contents)**3. ### What's the difference between using “let” and “var” to declare a variable in ES6?
Main difference is scoping rules. Variables declared by ``var`` keyword are scoped to the immediate function body (hence the function scope) while ``let`` variables are scoped to the immediate enclosing block denoted by `` { } `` (hence the block scope).
```typescript
function run() {
var foo = "Foo";
let bar = "Bar";console.log(foo, bar);
{
let baz = "Bazz";
console.log(baz);
}console.log(baz); // ReferenceError
}run();
```
**[⬆ Back to Top](#table-of-contents)**