Top JavaScript Interview Questions & Answers

The following features are available in Javascript-
  • Interpreted Language
  • Cross Platform
  • Object-Oriented Scripting Language
  • Asynchronous Processing
  • # Slice() Method Splice() Method
    1 It doesn't modify the original array(immutable). Modifies the original array(mutable).
    2 Returns the subset of the original array. Its returns the deleted elements as an array.
    3 It is used to pick the element from the array. It is used to insert or delete elements from an array.

    Netscape developed Javascript.

    Using the object literal, you can create an object like seen below:
                                    
                                        var emp = {
                                            name: "John",
                                            age: 19
                                            }; 
                                    
                                

    Undeclared variables are not declared and don't exist in a program. A runtime error occurs if the software tries to read the value of an undefined variable.

    Variables declared in the program but do not have a value are considered undefined variables. An undefined value is returned if the software tries to read the value of an undefined variable.

    # Windows Document
    1 The window of Javascript is a global object which holds variables, functions, locations, and history. This also comes under the window and can be understood as the property of the window.
    2 It is the representation of the user’s browser window and represents the content of the webpage. It is the representation of any HTML document that is loaded on the browser.
    3 Global objects, functions, and variables of JavaScript are part of the window object. The HTML tags, elements, and their attributes are members of the Document.

    JavaScript functions are written using arrow functions, which are clear and concise. An arrow function's general syntax is as follows:

    syntax:
                                    
                                        const welcome = () => {
                                            console.log("welcome to Coder Lite");
                                          };
                                    
                                

    The variable would be undefined when we declare a variable and do not give any value. If we want to have a variable with no value, we can assign a null value. Undefined represents no value, whereas null is a value.
                                    
                                        var a;
                                        var b= null;  
                                    
                                
    Here a is undefined, and b is null.

    The scope of the variable determines the variable accessibility in a particular function.

    There are two types of Variable Scopes in javascript:
  • Local Scope
  • Global Scope.

  • Local Scope: In this scope, the visibility or accessibility of a variable is limited to a specific function. Let’s understand it with an example:
                                    
                                        function ninjas() {
                                            var course = "Web Development";  // course is limited to ninjas because it is a local variable
                                          }
                                          function dev()
                                          {
                                          //dev cannot access the course variable because it is local to the ninjas() function
                                          }
                                    
                                

    Global Scope: We define a variable outside the function accessible for all other functions in this scope. This sets the variable scope global. Let’s understand it with an example:
                                    
                                        var course = "Web Development";  // It is a global variable
                                        function ninjas() {
                                        var getninjas = course;
                                        }
                                        function dev()
                                        {
                                        var getninjas = course;
                                        }
                                    
                                

    Undeclared variables are not declared and don't exist in a program. A runtime error occurs if the software tries to read the value of an undefined variable.

    Variables declared in the program but do not have a value are considered undefined variables. An undefined value is returned if the software tries to read the value of an undefined variable.

    By pressing the appropriate key, typically the F12 key, all contemporary web browsers, including Chrome, Firefox, and others, feature an integrated debugger that can be used anytime. The debugging tools provide users with a number of functions.

    The different ways to remove duplicates in Javascript are mentioned below:

    Filter method:
    Three arguments are specified to call the filter() function. These are the array, element in use, and element index.

    For loop:
    All the recurring elements are kept in an empty array.

    The symbols for comments are given below:
                                
                                    // for Single line comments and
    
                                    /* Multi
                                    Line
                                    Comment
                                    */
                                
                            
    Note that you can use an arrow function to define an IIFE:

    Example 1 :
                                
                                    (function(){
                                        let x="Ajay"
                                        console.log(x) // Ajay
                                      })();
                                
                            
    Example 2 :
                                
                                    (function(a,b){
                                        console.log(a+b)  // 30
                                        return a + b;
                                    })(10,20);
                                
                            
    Example 3 :
                                
                                    (function() {
                                        var counter = 0;
                                    
                                        function add(a, b) {
                                            return a + b;
                                        }
                                    
                                        console.log(add(10,20)); // 30
                                    }());
                                
                            
    Example 4 :
                                
                                    (function() {
                                        var counter = 0;
                                    
                                        function add(a, b) {
                                            return a + b;
                                        }
                                    
                                        console.log(add(10,20)); // 30
                                    }());
                                
                            
    Important Point
  • IIFE follow their own scope like any other function/variable in JavaScript.
  • The part of the name immediately invoked is sometimes confusing to new developers as they expect the IIFE to execute irrespective of function scope, this is wrong. For example, let us take the following example where the IIFE is defined within a function and will only be immediately invoked if we call it the Parent Function.
  • Example 5 :
                                
                                    function myFunc()
                                    {
                                        console.log("Welcome to");  // Welcome to
                                        // This will be executed after executing the previous log.
                                        (function() { 
                                            console.log("Ajay Kumar"); // Ajay Kumar
                                        })();
                                        console.log("Hi There!");  // Hi There!
                                    }
                                      
                                    
                                    myFunc();
                                
                            
  • 1. IIFEs have their own scope i.e. the variables you declare in the Function Expression will not be available outside the function.
  • 2. Similarly to other functions IIFEs can also be named or anonymous, but even if an IIFE does have a name it is impossible to refer/invoke it.
  • 3. IIFEs can also have parameters. For example,
  • Example 6 :
                                
                                    // Declaring the parameter required.
                                    (function(dt) {
                                     console.log(dt.toLocaleTimeString());  // 6:12:57 PM
                                     // Passing the Parameter.
                                 })(new Date());
                                
                            

    var is function-scoped, while let and const are block-scoped. Variables declared with var can be re-declared and updated, whereas let allows updates but not re-declarations. const declarations create variables that cannot be reassigned, but their contents can be mutated if the variable holds an object
                        
                            var x = 5;
                            x = 10; // Allowed
                            
                            let y = 5;
                            y = 10; // Allowed
                            
                            const z = 5;
                            z = 10; // Error