FrontendDeveloper.in

JavaScript Interview Questions

  • Question 91

    What is the difference between document load and DOMContentLoaded events

    The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for assets(stylesheets, images, and subframes) to finish loading. Whereas The load event is fired when the whole page has loaded, including all dependent resources(stylesheets, images).

  • Question 92

    What is the difference between native, host and user objects

    Native objects are objects that are part of the JavaScript language defined by the ECMAScript specification. For example, String, Math, RegExp, Object, Function etc core objects defined in the ECMAScript spec. Host objects are objects provided by the browser or runtime environment (Node).

    For example, window, XmlHttpRequest, DOM nodes etc are considered as host objects. User objects are objects defined in the javascript code. For example, User objects created for profile information.

  • Question 94

    What are the pros and cons of promises over callbacks

    Below are the list of pros and cons of promises over callbacks,

    Pros:

    1. It avoids callback hell which is unreadable
    2. Easy to write sequential asynchronous code with .then()
    3. Easy to write parallel asynchronous code with Promise.all()
    4. Solves some of the common problems of callbacks(call the callback too late, too early, many times and swallow errors/exceptions)

    Cons:

    1. It makes little complex code
    2. You need to load a polyfill if ES6 is not supported
  • Question 95

    What is the difference between an attribute and a property

    Attributes are defined on the HTML markup whereas properties are defined on the DOM. For example, the below HTML element has 2 attributes: type and value,

    <input type="text" value="Name:">
    

    You can retrieve the attribute value as below, for example after typing "Good morning" into the input field:

    const input = document.querySelector("input");
    console.log(input.getAttribute("value")); // Good morning
    console.log(input.value); // Good morning
    

    And after you change the value of the text field to "Good evening", it becomes like

    console.log(input.getAttribute("value")); // Good evening
    console.log(input.value); // Good evening
    
  • Question 96

    What is same-origin policy

    The same-origin policy is a policy that prevents JavaScript from making requests across domain boundaries. An origin is defined as a combination of URI scheme, hostname, and port number. If you enable this policy then it prevents a malicious script on one page from obtaining access to sensitive data on another web page using Document Object Model(DOM).

  • Question 97

    What is the purpose of void 0

    Void(0) is used to prevent the page from refreshing. This will be helpful to eliminate the unwanted side-effect, because it will return the undefined primitive value. It is commonly used for HTML documents that use href="JavaScript:Void(0);" within an <a> element. i.e, when you click a link, the browser loads a new page or refreshes the same page. But this behavior will be prevented using this expression. For example, the below link notify the message without reloading the page

    Click Me!
    
  • Question 98

    Is JavaScript a compiled or interpreted language

    JavaScript is an interpreted language, not a compiled language. An interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. Nowadays modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.

  • Question 99

    Is JavaScript a case-sensitive language

    Yes, JavaScript is a case sensitive language. The language keywords, variables, function & object names, and any other identifiers must always be typed with a consistent capitalization of letters.

  • Question 100

    Is there any relation between Java and JavaScript

    No, they are entirely two different programming languages and have nothing to do with each other. But both of them are Object Oriented Programming languages and like many other languages, they follow similar syntax for basic features(if, else, for, switch, break, continue etc).

  • Question 101

    What are events

    Events are "things" that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can react on these events. Some of the examples of HTML events are,

    1. Web page has finished loading
    2. Input field was changed
    3. Button was clicked

    Let's describe the behavior of click event for button element,

    <!doctype html>
    <html>
     <head>
    <script>
    function greeting() {
    alert('Hello! Good morning');
    }
    </script>
     </head>
     <body>
    <button type="button" onclick="greeting()">Click me</button>
     </body>
    </html>
    
  • Question 102

    Who created javascript

    JavaScript was created by Brendan Eich in 1995 during his time at Netscape Communications. Initially it was developed under the name Mocha, but later the language was officially called LiveScript when it first shipped in beta releases of Netscape.

  • Question 103

    What is the use of preventDefault method

    The preventDefault() method cancels the event if it is cancelable, meaning that the default action or behaviour that belongs to the event will not occur. For example, prevent form submission when clicking on submit button and prevent opening the page URL when clicking on hyperlink are some common use cases.

    document
    .getElementById("link")
    .addEventListener("click", function (event) {
    event.preventDefault();
    });
    

    Note: Remember that not all events are cancelable.

  • Question 104

    What is the use of stopPropagation method

    The stopPropagation method is used to stop the event from bubbling up the event chain. For example, the below nested divs with stopPropagation method prevents default event propagation when clicking on nested div(Div1)

    
    <script>
    function firstFunc(event) {
    alert("DIV 1");
    event.stopPropagation();
    }
    
    function secondFunc() {
    alert("DIV 2");
    }
    </script>
    
  • Question 105

    What are the steps involved in return false usage

    The return false statement in event handlers performs the below steps,

    1. First it stops the browser's default action or behaviour.
    2. It prevents the event from propagating the DOM
    3. Stops callback execution and returns immediately when called.
Get LinkedIn Premium at Rs 399