FrontendDeveloper.in

JavaScript Interview Questions

  • Question 331

    What paradigm is Javascript

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

  • Question 333

    Is JavaScript faster than server side script

    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.

  • Question 334

    How do you get the status of a checkbox

    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
    
  • Question 336

    How do you convert character to ASCII code

    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'
    
  • Question 337

    What is ArrayBuffer

    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);
    
  • Question 338

    What is the output of below string expression

    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.

  • Question 339

    What is the purpose of Error object

    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);
    }
    
  • Question 340

    What is the purpose of EvalError object

    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"
    
  • Question 341

    What are the list of cases error thrown from non-strict mode to strict mode

    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.

  • Question 342

    Do all objects have prototypes

    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.

  • Question 343

    What is the difference between a parameter and an argument

    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");
    
  • Question 344

    What is the purpose of some method in arrays

    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)
    
  • Question 345

    How do you combine two or more arrays

    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
    
Get LinkedIn Premium at Rs 399