JavaScript Interview Questions and Answers | Set 3

Javascript interview questions list
We have already discussed some interview questions based on JavaScript.

Below are some more related questions:

  1. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
    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 strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict” instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
  2.     

  3. How to get the status of a CheckBox ?
    The DOM Input Checkbox Property is used to set or return the checked status of a checkbox field. This property is used to reflect the HTML Checked attribute.

    document.getElementById("GFG").checked;

    If the CheckBox is checked then it returns True.

  4.     



  5. How to explain closures in JavaScript and when to use it ?
    The closure is created when a child functions to keep the environment of the parent’s scope even after the parent’s function has already executed. The Closure is a locally declared variable related to a function. The closure will provide better control over the code when using them.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Explanation of closure 
    function foo() { 
        var b = 1; 
        function inner() { 
            return b; 
        
        return inner; 
    var get_func_inner = foo();         
      
    console.log(get_func_inner()); 
    console.log(get_func_inner()); 
    console.log(get_func_inner()); 

    chevron_right

    
    

  6.     

  7. What is the difference between call() and apply() methods ?
    Both methods are used in a different situation

    • call() Method: It calls the method, taking the owner object as argument. The keyword this refers to the ‘owner’ of the function or the object it belongs to. We can call a method that can be used on different objects.
    • apply() Method: The apply() method is used to write methods, which can be used on different objects. It is different from the function call() because it takes arguments as an array.
  8.     

  9. How to target a particular frame from a hyperlink in JavaScript ?
    This can be done by using the target attribute in the hyperlink. Like

    <a href="/geeksforgeeks.htm" target="newframe">New Page</a>
  10.     

  11. Write the errors shown in JavaScript ?
    There are three different types of errors in javaScript.

    • Syntax error: A syntax error is an error in the syntax of a sequence of characters or tokens that are intended to be written in a particular programming language.
    • Logical error: It is the most difficult error to be traced as it is the error on the logical part of the coding or logical error is a bug in a program that causes to operate incorrectly and terminate abnormally.
    • Runtime Error: A runtime error is an error that occurs during the running of the program, also known as the exceptions.
  12.     



  13. What is the difference between JavaScript and Jscript ?
    JavaScript:

    • It is a scripting language developed by Netscape.
    • It is used to design the client and server-side applications.
    • It is completely independent of Java language.

    Jscript:

    • It is a scripting language developed by Microsoft.
    • It is used to design active online content for word wide web.
  14.     

  15. What does var myArray = [[]]; statement declares ?
    In JavaScript, this statement is used to declare a two-dimensional array.
  16.     

  17. How many ways an HTML element can be accessed in a JavaScript code ?
    There are four possible ways to access HTML element in JavaScript which are:

  18.     

  19. What is the difference between innerHTML & innerText ?
    The innerText property sets or returns the text content as plain text of the specified node, and all its descendants whereas the innerHTML property sets or returns the plain text or HTML contents in the elements. Unlike innerText, inner HTML lets you work with HTML rich text and doesn’t automatically encode and decode text.
  20.     

  21. What is an event bubbling in JavaScript ?
    Consider a situation an element is present inside another element and both of them handle an event. When an event occurs in bubbling, the innermost element handles the event first, then outer, and so on.
  22.     

  23. What will be the output of the following code ?
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    var X = { geeks : 1}; 
    var Output = (function() { 
        delete X.geeks; 
        return X.geeks; 
    })(); 
      
    console.log(output);

    chevron_right

    
    

    Here the delete will delete the property of the object. X is the object with the geeks property and it is a self-invoking function that will delete the geeks property from object X so the result will be undefined.

  24.     

  25. How are JavaScript and ECMA Script related ?
    The JavaScript is the main language that has to maintain some rules and regulation that is ECMA Script, that rules also bring new features for the language JavaScript.
  26.     



  27. How to hide JavaScript code from old browsers that don’t support JavaScript ?
    To hide the JavaScript codes from the old browsers that don’t support JavaScript you can use

    <!-- before <script> tag and another //--> after </script> tag

    all the old browsers will take that as long comment of HTML. New browsers that support JavaScripts will take that as an online comment.

  28.     

  29. What will be the output of the following code ?
    var output = (function(x) {
        delete x;
        return x;
    })(0);
      
    document.write(output);

    The output will be 0. The delete operator is used to delete the operator of the object but the X is not the object here it is a local variable. The delete operator doesn’t affect local variables.

  30.     

  31. In JavaScript, answer if the following expressions result in true or false.
    "0" == 0   // true or false ? 
    "" == 0   // true or false ? 
    "" == "0"   // true or false ?
    • The result will be True for 1st and 2nd case and False for the 3rd case.
  32.     

  33. How to use any browser for debugging ?
    By pressing the F12 we can trigger the debugging mode of any browser and can view the result by taping the console.
  34.     

  35. What is javascript Hoisting ?
    When any interpreter runs the code then all the variables re-hoisted to the top of the original scope. This method is applicable for declaration not for the initialization of a variable. This is known as a javascript Hoisting.
  36.     

  37. What is the syntax of ‘Self Invoking Function’ ?
    The syntax for Self INvoking Function: The last bracket contains the function expression.

    (function () {
        return () 
    } () ;
  38.     

  39. How to use external JavaScript file in another JavaScript file ?
    You can use the below code to use external JavaScript code into another JavaScript file.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var script = document.createElement('script'); 
                
        script.src = "external javascript file"
                
        document.head.appendChild(script) 
    </script> 

    chevron_right

    
    

    1. full-stack-img




      My Personal Notes arrow_drop_up

      Check out this Author's contributed articles.

      If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

      Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



      Improved By : Akanksha_Rai