FrontendDeveloper.in

JavaScript Interview Questions

  • Question 61

    What are the events available for server sent events

    Below are the list of events available for server sent events

    EventDescription
    onopenIt is used when a connection to the server is opened
    onmessageThis event is used when a message is received
    onerrorIt happens when an error occurs
  • Question 62

    What are the main rules of promise

    A promise must follow a specific set of rules:

    1. A promise is an object that supplies a standard-compliant .then() method
    2. A pending promise may transition into either fulfilled or rejected state
    3. A fulfilled or rejected promise is settled and it must not transition into any other state.
    4. Once a promise is settled, the value must not change.
  • Question 63

    What is callback in callback

    You can nest one callback inside in another callback to execute the actions sequentially one by one. This is known as callbacks in callbacks. Beware, too many levels of nesting lead to Callback hell

    loadScript("/script1.js", function (script) {
    console.log("first script is loaded");
    
    loadScript("/script2.js", function (script) {
    console.log("second script is loaded");
    
    loadScript("/script3.js", function (script) {
    console.log("third script is loaded");
    // after all scripts are loaded
    });
    });
    });
    
  • Question 64

    What is promise chaining

    The process of executing a sequence of asynchronous tasks one after another using promises is known as Promise chaining. Let's take an example of promise chaining for calculating the final result,

    new Promise(function (resolve, reject) {
    setTimeout(() => resolve(1), 1000);
    })
    .then(function (result) {
    console.log(result); // 1
    return result * 2;
    })
    .then(function (result) {
    console.log(result); // 2
    return result * 3;
    })
    .then(function (result) {
    console.log(result); // 6
    return result * 4;
    });
    

    In the above handlers, the result is passed to the chain of .then() handlers with the below work flow,

    1. The initial promise resolves in 1 second,
    2. After that .then handler is called by logging the result(1) and then return a promise with the value of result * 2.
    3. After that the value passed to the next .then handler by logging the result(2) and return a promise with result * 3.
    4. Finally the value passed to the last .then handler by logging the result(6) and return a promise with result * 4.
  • Question 65

    What is promise.all

    Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected. For example, the syntax of promise.all method is below,

    Promise.all([Promise1, Promise2, Promise3]) .then(result) => {   console.log(result) }) .catch(error => console.log(`Error in promises ${error}`))
    

    Note: Remember that the order of the promises(output the result) is maintained as per input order.

  • Question 66

    What is the purpose of the race method in promise

    Promise.race() method will return the promise instance which is firstly resolved or rejected. Let's take an example of race() method where promise2 is resolved first

    var promise1 = new Promise(function (resolve, reject) {
    setTimeout(resolve, 500, "one");
    });
    var promise2 = new Promise(function (resolve, reject) {
    setTimeout(resolve, 100, "two");
    });
    
    Promise.race([promise1, promise2]).then(function (value) {
    console.log(value); // "two" // Both promises will resolve, but promise2 is faster
    });
    
  • Question 67

    What is a strict mode in javascript

    JavaScript’s "use strict" directive is used to opt into a stricter parsing and error-handling mode for your scripts or functions. It helps catch common bugs, makes your code more secure, and prepares it for future versions of JavaScript.

    Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions. The literal expression "use strict"; instructs the browser to use the javascript code in the Strict mode. This also enables block-scoped variables.

  • Question 68

    Why do you need strict mode

    Strict mode is useful to write "secure" JavaScript by notifying "bad syntax" into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.

  • Question 69

    How do you declare strict mode

    The strict mode is declared by adding "use strict"; to the beginning of a script or a function. If declared at the beginning of a script, it has global scope.

    "use strict";
    x = 3.14; // This will cause an error because x is not declared
    

    and if you declare inside a function, it has local scope

    x = 3.14; // This will not cause an error.
    myFunction();
    
    function myFunction() {
    "use strict";
    y = 3.14; // This will cause an error
    }
    
  • Question 70

    What is the purpose of double exclamation

    The double exclamation or negation(!!) ensures the resulting type is a boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, it will be true. For example, you can test IE version using this expression as below,

    let isIE8 = false;
    isIE8 = !!navigator.userAgent.match(/MSIE 8.0/);
    console.log(isIE8); // returns true or false
    

    If you don't use this expression then it returns the original value.

    console.log(navigator.userAgent.match(/MSIE 8.0/)); // returns either an Array or null
    

    Note: The expression !! is not an operator, but it is just twice of ! operator.

  • Question 71

    What is the purpose of the delete operator

    The delete operator is used to delete the property as well as its value.

    var user = { firstName: "John", lastName: "Doe", age: 20 };
    delete user.age;
    
    console.log(user); // {firstName: "John", lastName:"Doe"}
    
  • Question 72

    What is typeof operator

    You can use the JavaScript typeof operator to find the type of a JavaScript variable. It returns the type of a variable or an expression.

    typeof "John Abraham"; // Returns "string"
    typeof (1 + 2); // Returns "number"
    typeof [1, 2, 3]; // Returns "object" because all arrays are also objects
    
  • Question 73

    What is undefined property

    The undefined property indicates that a variable has not been assigned a value, or declared but not initialized at all. The type of undefined value is undefined too.

    var user; // Value is undefined, type is undefined
    console.log(typeof user); //undefined
    

    Any variable can be emptied by setting the value to undefined.

    user = undefined;
    
  • Question 74

    What is null value

    The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values. The type of null value is object. You can empty the variable by setting the value to null.

    var user = null;
    console.log(typeof user); //object
    
  • Question 75

    What is the difference between null and undefined

    Below are the main differences between null and undefined,

    NullUndefined
    It is an assignment value which indicates that variable points to no object.It is not an assignment value where a variable has been declared but has not yet been assigned a value.
    Type of null is objectType of undefined is undefined
    The null value is a primitive value that represents the null, empty, or non-existent reference.The undefined value is a primitive value used when a variable has not been assigned a value.
    Indicates the absence of a value for a variableIndicates absence of variable itself
    Converted to zero (0) while performing primitive operationsConverted to NaN while performing primitive operations
Get LinkedIn Premium at Rs 399