JavaScript Interview questions and answers part 2

 


What is function?

  • Function is collection of statements which can be used anywhere in program, it is used to perform specific task.
What are type of function?
    Two type of function 

  • Named function : Function which is have name at the time of definition are named function 
    function myFun(){
            console.log('This is a name function');
         }
  •  Anonymous Function: Which do not have names are anonymous functions.
    var myFun = function(){
            console.log('Anomymouse Function')
        }
What is isNaNa?
  • The isNaNa() function is used to determines whether a value is an illegal number (Not a Number)
  • The function returns true if the value equates to NaNa. otherwise it return false.
    var a = '50';
     var b = 10;
     var c = 20;
     var d = 'Pradeep';
     console.log(isNaN(a)) // Output: false
     console.log(isNaN(b)) // Output: false
     console.log(isNaN(c)) // Output: false     
     console.log(isNaN(d)) // Output: true
What is strict Mode?
  • Strict mode prevents certain actions and throws more exceptions. The statement "use strict" orders browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
  • Strict mode eliminates some silent errors in JavaScript by changing them to throw errors
  • Strict mode resolves mistakes that make it difficult for JavaScript engines to perform optimizations hence strict mode code can sometimes run faster than identical code that’s not strict mode.
  • Strict mode forbids some syntax likely to be defined in future versions of ECMAScript.
  • Due to Strict mode it becomes easier to write secure JavaScript.
  • To invoke strict mode for a function, put statement "use strict" in the function's body before any other statements.
What is callback functions?
  •  A function passed into another function as an argument, which is then invoked inside the outer function to complete some kind action is called as callback function.
    function showName(name){
        alert('User Name is : '+ name)
        }
        function displayName(callback){
         var name = prompt('Please Enter Your Name');
        callback(name)   
        }
        displayName(showName);

What is setTimeout function?
  • The setTimeout() function executes function at specified interval. syntex setTimeout(expression, timeout);
  • Here, expression is the function/code that is called only once and timeout is number of milliseconds to wait before calling the function.
  • The clearTimeout() function is used to deactivate or cancel timer set by setTimeout() fuction.
What is setInterval function?
  • The setInterval() function executes a function after a specified time interval.
  • setInterval(expression, timeout);
  • Here, expression specifies function/code to be called after particular time interval and timeout specifies the time interval between function calls.
  • The clearInterval() function is used to cancel or deactivate the timer set by setInterval() function.
What is difference between setInterval and setTimeout functions?
  • The setTimeout(expression, timeout) runs the function once after timeout
  • The setInterval(expression, timeout) runs the function in intervals repeatedly, with length of timeout between them.
What are Events?
  • Events are actions or occurrences that happen in the system you are programming to which you can respond in some way. Events are handled by function know as event handler.
What are different events in JavaScript?
  • Input Event
    1. onsubmit - triggers on submitting a form.
    2. onselect - triggers on selecting an element
    3. onchange - triggers when changes happen to an element.
    4. onfocus - triggers when windows gets focus.
    5. onreset - triggers when user clicks reset button.
    6 onblur - triggers when window loses focus.
    7. onkeyup - triggers on releasing a key
    8. onkeydown - triggers on pressing a key.
  • Click Event
    1. onclick - trigger on clicking a mouse button.
    2. ondblclick - triggers on double clicking mouse button.
  • Mouse Event
    1. ondrag - triggers when element is dragged
    2. ondragend - triggers when drag ends
    3. ondragstart - triggers when drag starts
    4. ondragenter - triggers when dragged element is dropped
    5. ondragleave - triggers on leaving target while dragging element
    6. onmouseover - triggers when mouse pointer moves over element
    7. onmousedown - triggers on pressing mouse button.
    8. onmouseup - triggers on releasing mouse button
    9. onscroll - triggers on scrolling a scroll bar of an element
  • Load Event
    1. onload- triggers when page has been loaded.
    2. onerror- triggers when an error occurs when loading an image.
    3. onunload- triggers when browser closes document.
What is addEventListener() method?
  • The addEventListener() method attaches an event handler to specified element. You can add multiple event handlers to one element. It is possible to add event listener to any DOM object.
    document.getElementById("someUniqueDivId").addEventListener("click", respondtoclick);
        function respondtoclick() {
        console.log("Do some stuff!!!");
        }
What is event bubbling?
  • When an event happens on an element, it first runs the handlers on that particular element, after that handlers on its parent runs, this happens all the way up on all other ancestors.
  • This bubbling of events from child to parent is called event bubbling.
  • A click on inner <p> first runs onclick on that <p>, then on outer <div>, then on outer <form> and so on till document object.

What is Object?
  • The object is collection of properties and methods.
  • Object in JavaScript are variables as well. Object can have properties any data types (String, Number, Boolean etc.).
  • Object properties can be primitive values, other objects and functions.
    var book = {
         name : "Javascript Book",
         auther: Pradeep Bhosle",
         pages: 100
        }
Using new keyword with built-in user defined constructor function:
  • We first create a constructor function and then get objects using ‘new’ keyword. 
     function Book(name, author, pages) {
        this.name = name;
        this.author = author;
        this.pages = pages;
        }
        var book = new Book("Javascript Book", "Pradeep Bhosle", 100);

What is 'this' keyword?
  • The this keyword refers to the object it belongs to
  • In an object method, this refers to the object to which method belongs.
  • When used alone, the owner is the Global object, so this refers to the Global object (Windows object).
  • In strict mode, when used in a function, this is undefined.
What is difference between call() and apply()?
  • The Function.prototype.call() method calls a function with a provided this value and arguments provided individually.
  • It is necessary to know arguments of function when using call() method.
  • The Function.prototype.apply() method calls a function with a provided this value, and arguments provided as an array (or an array-like object).
  • It is necessary to know arguments of function when using apply() method.
What is difference between attribute and property?
  • Attributes: Provide more details on an element like id, type, value etc.
  • Property: Value assigned to the property like type=”text”, value=’Name’ etc.
What is cookie?
  • The Cookies are small items of data that consists of name and value pair.
  • Cookies are stored on your computer so that it can be accessed by your web browser.
  •  A web browser and server communicate through HTTP which is stateless protocol. Stateless protocol treats each request independently, so server does not keep data after sending it to browser. With cookies such data can be fetched directly from stored cookie file instead of communicating with server.
  • For example when user visits web page, user name can be stored in cookie. Now when next time user visits the page cookie belonging to the page is added to the request. This way server gets necessary data “remembered” by cookie.
How cookie helps client server HTTP communication?
  • When a user sends a request to the server, then each of that request is treated as a new request sent by the different user.
  • When receiving an HTTP request, a server can possibly send a SetCookie header with the response.
  •  Now, whenever a user sends a request to the server, the cookie is added with that request automatically. Due to the cookie, the server recognizes the users
What is difference between local storage and session storage?
  • Local Storage : For every HTTP request, the data is not sent back to the server (HTML, images, JavaScript, CSS, etc) reducing the total traffic between client and server. Data will stay until it is manually cleared using settings or through program
  • Session Storage : It is similar to local storage; the only difference is data stored in local storage has no expiration time whereas data stored in
  • session storage gets cleared when the page session ends. Session Storage will cleared when the browser is closed.
What are different error types in JavaScript?
  • SyntaxError : Raised when syntax error occurs while parsing the JavaScript code.
  • RangeError : Raised when numeric value exceeds allowed range.
  • EvalError : Raised when the eval() function is used in an incorrect manner.
  • ReferenceError : Raised when an invalid reference is used
  • TypeError : Raised when type of variable is not as expected.
  • URIError : Raised when the encodeURI() or decodeURI() functions are used in an inaccurate manner.
  • InternalError : Raised when internal error in the JavaScript engine is thrown.

Comments