FrontendDeveloper.in

JavaScript Interview Questions

  • Question 226

    What are the function parameter rules

    JavaScript functions follow below rules for parameters,

    1. The function definitions do not specify data types for parameters.
    2. Do not perform type checking on the passed arguments.
    3. Do not check the number of arguments received. i.e, The below function follows the above rules,
    function functionName(parameter1, parameter2, parameter3) {
    console.log(parameter1); // 1
    }
    functionName(1);
    
  • Question 227

    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,

    try {
    greeting("Welcome");
    } catch (err) {
    console.log(err.name + "
    " + err.message);
    }
    
  • Question 228

    When do you get a syntax error

    A SyntaxError is thrown if you try to evaluate code with a syntax error. For example, the below missing quote for the function parameter throws a syntax error

    try {
    eval("greeting('welcome)"); // Missing ' will produce an error
    } catch (err) {
    console.log(err.name);
    }
    
  • Question 229

    What are the different error names from error object

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

    Error NameDescription
    AggregateErrorAn error indicating that multiple errors occurred
    EvalErrorAn error has occurred in the eval() function
    RangeErrorAn error has occurred with a number "out of range"
    ReferenceErrorAn error due to an illegal reference
    SyntaxErrorAn error due to a syntax error
    TypeErrorAn error due to a type error
    URIErrorAn error due to encodeURI()
  • Question 230

    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.
  • Question 231

    What are the two types of loops in javascript

    1. Entry Controlled loops: In this kind of loop type, the test condition is tested before entering the loop body. For example, For Loop and While Loop comes under this category.
    2. Exit Controlled Loops: In this kind of loop type, the test condition is tested or evaluated at the end of the loop body. i.e, the loop body will execute at least once irrespective of test condition true or false. For example, do-while loop comes under this category.
  • Question 232

    What is nodejs

    Node.js is a server-side platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. It is an event-based, non-blocking, asynchronous I/O runtime that uses Google's V8 JavaScript engine and libuv library.

  • Question 233

    What is the Intl object

    The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. It provides access to several constructors and language sensitive functions.

  • Question 234

    How do you perform language specific date and time formatting

    You can use the Intl.DateTimeFormat object which is a constructor for objects that enable language-sensitive date and time formatting. Let's see this behavior with an example,

    var date = new Date(Date.UTC(2019, 07, 07, 3, 0, 0));
    console.log(new Intl.DateTimeFormat("en-GB").format(date)); // 07/08/2019
    console.log(new Intl.DateTimeFormat("en-AU").format(date)); // 07/08/2019
    
  • Question 235

    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).

  • Question 236

    How does synchronous iteration works

    Synchronous iteration was introduced in ES6 and it works with below set of components,

    Iterable: It is an object which can be iterated over via a method whose key is Symbol.iterator.

    Iterator: It is an object returned by invoking [Symbol.iterator]() on an iterable. This iterator object wraps each iterated element in an object and returns it via next() method one by one.

    IteratorResult: It is an object returned by next() method. The object contains two properties; the value property contains an iterated element and the done property determines whether the element is the last element or not.

    Let's demonstrate synchronous iteration with an array as below

    const iterable = ["one", "two", "three"];
    const iterator = iterable[Symbol.iterator]();
    console.log(iterator.next()); // { value: 'one', done: false }
    console.log(iterator.next()); // { value: 'two', done: false }
    console.log(iterator.next()); // { value: 'three', done: false }
    console.log(iterator.next()); // { value: 'undefined, done: true }
    
  • Question 237

    What is the event loop

    The event loop is a process that continuously monitors both the call stack and the event queue and checks whether or not the call stack is empty. If the call stack is empty and there are pending events in the event queue, the event loop dequeues the event from the event queue and pushes it to the call stack. The call stack executes the event, and any additional events generated during the execution are added to the end of the event queue.

    Note: The event loop allows Node.js to perform non-blocking I/O operations, even though JavaScript is single-threaded, by offloading operations to the system kernel whenever possible. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background.

  • Question 238

    What is the call stack

    Call Stack is a data structure for javascript interpreters to keep track of function calls(creates execution context) 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 state representation in a diagram format

    function hungry() {
    eatFruits();
    }
    function eatFruits() {
    return "I'm eating fruits";
    }
    
    // Invoke the `hungry` function
    hungry();
    

    The above code processed in a call stack as below,

    1. Add the hungry() function to the call stack list and execute the code.
    2. Add the eatFruits() function to the call stack list and execute the code.
    3. Delete the eatFruits() function from our call stack list.
    4. Delete the hungry() function from the call stack list since there are no items anymore.

    Screenshot

  • Question 239

    What is the event queue

    The event queue follows the queue data structure. It stores async callbacks to be added to the call stack. It is also known as the Callback Queue or Macrotask Queue.

    Whenever the call stack receives an async function, it is moved into the Web API. Based on the function, Web API executes it and awaits the result. Once it is finished, it moves the callback into the event queue (the callback of a promise is moved into the microtask queue).

    The event loop constantly checks whether or not the call stack is empty. Once the call stack is empty and there is a callback in the event queue, the event loop moves the callback into the call stack. But if there is a callback in the microtask queue as well, it is moved first. The microtask queue has a higher priority than the event queue.

  • Question 240

    What is a decorator

    A decorator is an expression that evaluates to a function and that takes the target, name, and decorator descriptor as arguments. Also, it optionally returns a decorator descriptor to install on the target object. Let's define admin decorator for user class at design time,

    function admin(isAdmin) {
    return function(target) {
    target.isAdmin = isAdmin;
    }
    }
    
    @admin(true)
    class User() {
    }
    console.log(User.isAdmin); //true
    
     @admin(false)
     class User() {
     }
     console.log(User.isAdmin); //false
    
Get LinkedIn Premium at Rs 399