FrontendDeveloper.in

JavaScript Interview Questions

Filter by topic:
  • Yes, you can create a proper random function to return a random number between min and max (both included)

    function randomInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    randomInteger(1, 100); // returns a random integer from 1 to 100
    randomInteger(1, 1000); // returns a random integer from 1 to 1000
    
  • Tree shaking is a form of dead code elimination. It means that unused modules will not be included in the bundle during the build process and for that it relies on the static structure of ES2015 module syntax,( i.e. import and export). Initially this has been popularized by the ES2015 module bundler rollup, these days practically all bundlers use this technique.

  • Tree Shaking can significantly reduce the code size in any application. i.e, The less code we send over the wire the more performant the application will be. For example, if we just want to create a “Hello World” Application using SPA frameworks then it will take around a few MBs, but by tree shaking it can bring down the size to just a few hundred KBs. Tree shaking is implemented in Rollup and Webpack bundlers.

  • No, it allows arbitrary code to be run which causes a security problem. As we know that the eval() function is used to run text as code. In most of the cases, it should not be necessary to use it.

  • A regular expression is a sequence of characters that forms a search pattern. You can use this search pattern for searching data in a text. These can be used to perform all types of text search and text replace operations. Let's see the syntax format now,

    /pattern/modifiers;
    

    For example, the regular expression or search pattern with case-insensitive username would be,

    /John/i;
    
  • There are six string methods: search(), replace(), replaceAll(), match(), matchAll(), and split().

    The search() method uses an expression to search for a match, and returns the position of the match.

    var msg = "Hello John";
    var n = msg.search(/John/i); // 6
    

    The replace() and replaceAll() methods are used to return a modified string where the pattern is replaced.

    var msg = "ball bat";
    var n1 = msg.replace(/b/i, "c"); // call bat
    var n2 = msg.replaceAll(/b/i, "c"); // call cat
    

    The match() and matchAll() methods are used to return the matches when matching a string against a regular expression.

    var msg = "Hello John";
    var n1 = msg.match(/[A-Z]/g); // ["H", "J"]
    var n2 = msg.matchAll(/[A-Z]/g); // this returns an iterator
    

    The split() method is used to split a string into an array of substrings, and returns the new array.

    var msg = "Hello John";
    var n = msg.split(/\s/); // ["Hello", "John"]
    
  • Modifiers can be used to perform case-insensitive and global searches. Let's list some of the modifiers,

    ModifierDescription
    iPerform case-insensitive matching
    gPerform a global match rather than stops at first match
    mPerform multiline matching

    Let's take an example of global modifier,

    var text = "Learn JS one by one";
    var pattern = /one/g;
    var result = text.match(pattern); // one,one
    
  • Regular Expressions provide a group of patterns in order to match characters. Basically they are categorized into 3 types,

    1. Brackets: These are used to find a range of characters. For example, below are some use cases,
    2. [abc]: Used to find any of the characters between the brackets(a,b,c)
    3. [0-9]: Used to find any of the digits between the brackets
    4. (a|b): Used to find any of the alternatives separated with |
    5. Metacharacters: These are characters with a special meaning. For example, below are some use cases,
    6. \d: Used to find a digit
    7. \s: Used to find a whitespace character
    8. \b: Used to find a match at the beginning or ending of a word
    9. Quantifiers: These are useful to define quantities. For example, below are some use cases,
    10. n+: Used to find matches for any string that contains at least one n
    11. n*: Used to find matches for any string that contains zero or more occurrences of n
    12. n?: Used to find matches for any string that contains zero or one occurrences of n
  • RegExp object is a regular expression object with predefined properties and methods. Let's see the simple usage of RegExp object,

    var regexp = new RegExp("\\w+");
    console.log(regexp);
    // expected output: /\w+/
    
  • You can use the test() method of regular expression in order to search a string for a pattern, and return true or false depending on the result.

    var pattern = /you/;
    console.log(pattern.test("How are you?")); //true
    
  • The purpose of exec method is similar to test method but it executes a search for a match in a specified string and returns a result array, or null instead of returning true/false.

    var pattern = /you/;
    console.log(pattern.exec("How are you?")); //["you", index: 8, input: "How are you?", groups: undefined]
    
  • You can change inline style or classname of a HTML element using javascript DOM-manipulation

    1. Using style property: You can modify inline style using style property
    document.getElementById("title").style.fontSize = "30px";
    
    1. Using ClassName property: It is easy to modify element class using className property
    document.getElementById("title").className = "custom-title";
    
  • The output is going to be 33. Since 1 and 2 are numeric values, the result of the first two digits is going to be a numeric value 3. The next digit is a string type value because of that the addition of numeric value 3 and string type value 3 is just going to be a concatenation value 33. Other operations like 3 * '3' do yield correct results because the string is coerced into a number.

  • The debugger statement invokes any available debugging functionality, such as setting a breakpoint. If no debugging functionality is available, this statement has no effect. For example, in the below function a debugger statement has been inserted. So execution is paused at the debugger statement just like a breakpoint in the script source.

    function getProfile() {
    // code goes here
    debugger;
    // code goes here
    }
    
  • You can set breakpoints in the javascript code once the debugger statement is executed and the debugger window pops up. At each breakpoint, javascript will stop executing, and let you examine the JavaScript values. After examining values, you can resume the execution of code using the play button.