FrontendDeveloper.in

JavaScript Interview Questions

Filter by topic:
  • JavaScript is a multi-paradigm language, supporting imperative/procedural programming, Object-Oriented Programming and functional programming. JavaScript supports Object-Oriented Programming with prototypical inheritance.

  • Yes, JavaScript is faster than server side scripts. Because JavaScript is a client-side script it does not require any web server’s help for its computation or calculation. So JavaScript is always faster than any server-side script like ASP, PHP, etc.

  • You can apply the checked property on the selected checkbox in the DOM. If the value is true it means the checkbox is checked, otherwise it is unchecked. For example, the below HTML checkbox element can be access using javascript as below:

    <input type="checkbox" id="checkboxname" value="Agree" />
    Agree the conditions
    
    
    console.log(document.getElementById(‘checkboxname’).checked); // true or false
    
  • You can use the String.prototype.charCodeAt() method to convert string characters to ASCII numbers. For example, let's find ASCII code for the first letter of 'ABC' string,

    "ABC".charCodeAt(0); // returns 65
    

    Whereas String.fromCharCode() method converts numbers to equal ASCII characters.

    String.fromCharCode(65, 66, 67); // returns 'ABC'
    
  • An ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. You can create it as below,

    let buffer = new ArrayBuffer(16); // create a buffer of length 16
    alert(buffer.byteLength); // 16
    

    To manipulate an ArrayBuffer, we need to use a “view” object.

    //Create a DataView referring to the buffer
    let view = new DataView(buffer);
    
  • console.log("Welcome to JS world"[0]);
    

    The output of the above expression is "W". Explanation: The bracket notation with specific index on a string returns the character at a specific location. Hence, it returns the character "W" of the string. Since this is not supported in IE7 and below versions, you may need to use the .charAt() method to get the desired result.

  • The Error constructor creates an error object and the instances of error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. The syntax of error object would be as below,

    new Error([message[, fileName[, lineNumber]]])
    

    You can throw user defined exceptions or errors using Error object in try...catch block as below,

    try {
    if (withdraw > balance)
    throw new Error("Oops! You don't have enough balance");
    } catch (e) {
    console.log(e.name + ": " + e.message);
    }
    
  • The EvalError object indicates an error regarding the global eval() function. Even though this exception is not thrown by JavaScript anymore, the EvalError object remains for compatibility. The syntax of this expression would be as below,

    new EvalError([message[, fileName[, lineNumber]]])
    

    You can throw EvalError with in try...catch block as below,

    try {
    throw new EvalError('Eval function error', 'someFile.js', 100);
    } catch (e) {
    console.log(e.message, e.name, e.fileName);              // "Eval function error", "EvalError", "someFile.js"
    
  • When you apply 'use strict'; syntax, some of the below cases will throw a SyntaxError before executing the script

    1. When you use Octal syntax
    var n = 022;
    
    1. Using with statement
    2. When you use delete operator on a variable name
    3. Using eval or arguments as variable or function argument name
    4. When you use newly reserved keywords
    5. When you declare a function in a block and access it from outside of the block
    if (someCondition) {
    function f() {}
    }
    f(); // ReferenceError: f is not defined
    

    Hence, the errors from above cases are helpful to avoid errors in development/production environments.

  • No. All objects have prototypes except two exceptions:

    • Object.prototype itself — This is the base object in the prototype chain, and its prototype is null.
    • Objects created with **Object.create(null)** — These are deliberately created with no prototype, so they don’t inherit from Object.prototype.

    All other standard objects do have a prototype.

  • Parameter is the variable name of a function definition whereas an argument represents the value given to a function when it is invoked. Let's explain this with a simple function

    function myFunction(parameter1, parameter2, parameter3) {
    console.log(arguments[0]); // "argument1"
    console.log(arguments[1]); // "argument2"
    console.log(arguments[2]); // "argument3"
    }
    myFunction("argument1", "argument2", "argument3");
    
  • The some() method is used to test whether at least one element in the array passes the test implemented by the provided function. The method returns a boolean value. Let's take an example to test for any odd elements,

    var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    var odd = (element) => element % 2 !== 0;
    
    console.log(array.some(odd)); // true (the odd element exists)
    
  • The concat() method is used to join two or more arrays by returning a new array containing all the elements. The syntax would be as below,

    array1.concat(array2, array3, ..., arrayX)
    

    Let's take an example of array's concatenation with veggies and fruits arrays,

    var veggies = ["Tomato", "Carrot", "Cabbage"];
    var fruits = ["Apple", "Orange", "Pears"];
    var veggiesAndFruits = veggies.concat(fruits);
    console.log(veggiesAndFruits); // Tomato, Carrot, Cabbage, Apple, Orange, Pears