Few important Questions in JavaScript and ES6

Ahad Hossain Aiman
4 min readMay 6, 2021

--

Short Introduction

1. What is an Iterator?

An iterator is an object which defines a sequence and a return value upon its termination. It implements the Iterator protocol with a next() method which returns an object with two properties: value (the next value in the sequence) and done (which is true if the last value in the sequence has been consumed.

Example: Use ES6 class,

2. What is an event loop?

The Event Loop is a queue of callback functions. When an async function executes, the callback function is pushed into the queue. The JavaScript engine doesn’t start processing the event loop until the async function has finished executing the code.

Note: It allows Node.js to perform non-blocking I/O operations even though JavaScript is single-threaded.

3. What is call stack?

Call Stack is a data structure for JavaScript interpreters to keep track of function calls in the program. It has two major actions,

  1. Whenever you call a function for its execution, you are pushing it to the stack.
  2. Whenever the execution is completed, the function is popped out of the stack.

Let’s take an example, and it’s a state representation in a diagram format

4. What is an error object?

An error object is a built-in error object that provides error information when an error occurs. It has two properties: name and message. For example, the below function logs error details,

4. What are the different error names from error object?

There are 6 different types of error names returned from error object,

  1. EvalError : An error has occurred in the eval() function
  2. RangeError : An error has occurred with a number “out of range”
  3. ReferenceError : An error due to an illegal reference
  4. SyntaxError : An error due to a syntax error
  5. TypeError : An error due to a type error
  6. URIError : An error due to encodeURI()

5. What are the various statements in error handling.

Below are the list of statements used in an error handling,

  1. try: This statement is used to test a block of code for errors
  2. catch: This statement is used to handle the error
  3. throw: This statement is used to create custom errors.
  4. finally: This statement is used to execute code after try and catch regardless of the result.

6. What are primitive data types

A primitive data type is data that has a primitive value (which has no properties or methods). There are 7 types of primitive data types.

  1. String
  2. number
  3. boolean
  4. null
  5. undefined
  6. bigint
  7. symbol

7. What is Hoisting?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Remember that JavaScript only hoists declarations, not initialisation. Let’s take a simple example of variable hoisting,

The above code looks like as below to the interpreter,

8. What are classes in ES6?

In ES6, JavaScript classes are primarily syntactic sugar over JavaScript’s existing prototype-based inheritance. For example, the prototype based inheritance written in function expression as below,

Whereas ES6 classes can be defined as an alternative

9. What are closures?

A closure is the combination of a function and the lexical environment within which that function was declared. i.e, It is an inner function that has access to the outer or enclosing function’s variables. The closure has three scope chains

  1. Own scope where variables defined between its curly brackets
  2. Outer function’s variables
  3. Global variables

Let’s take an example of the closure concept,

As per the above code, the inner function(i.e, greetingInfo) has access to the variables in the outer function scope(i.e, Welcome) even after the outer function has returned.

10. Arrow functions?

The arrow functions provides a more concise syntax for writing function expressions by opting out the function and return keywords using fat arrow(⇒) notation. Let’s see how this arrow function looks like,

You can also skip parenthesis(()) if the function has exactly one parameter(either zero or more than one parameter). Apart from this, you can wrap braces({}) if the function has more than one expression in the body.

Let’s list down all the variations of arrow functions,

--

--

Ahad Hossain Aiman

I am Aiman. I am a web developer. I can learn anything at any time. For that, I take the help of Google. My core skill is based on JavaScript .