ALIEN JAVASCRIPT INTERVIEW QUESTIONS

1) Strict Mode in javascript
  •  If enabled it throws error for potentially unsafe actions.
  •  It enabled javascript code runs faster as silly mistakes get eliminated while coding which may harm javascript performance
  Eample :
   function foo(){
    "use strict";
    // ... your code ...
   }

2) What is === operator in JavaScript and how it is different from == operator?
  •  == Checks if only the value if equal
  •  === checks if the value and type both are equal

3) Undefined vs Null
  •  Undefined - Variable declared but no value assigned ex. var s;
  •  null - null is the value that can be assigned to the variable to imply that the variable does not contain any value

4) Creating object/classes in javascript
  You can do it with json object notation with key / value pair
  
       Ex :
       var s= {
                      fname : 'test',
                      printval : function(){
                                    //----Action ----
                       }
                  }
  
       To call  : s.printval();

  Another way using function

       var s = new function(){
                     this.fname : 'test',
                     this.printval : function{
                                          //----Action----
                      }
                   }
        To call : s.printval();

5) if 2 methods have same name and same number of parameters, which one will be executed first?
2nd method

6) Difference between window.onload and onDocumentReady?
Window.onload will be called when each n every element including images n css is loaded which inturn causes more delay , where as ondocumentready will be called once the DOM is ready and able to be manipulated

7) Doc Type
Its basically  instruction to the web browser about what version of HTML the page is written in.
HTML 4 refers DTD which helps the browser abt rendering of the content
  • Strict.dtd : It does not include deprecated elements and framesets
  • Transitional.dtd : This DTD contains all HTML elements and attributes even deprecated not frameset
  • Frameset.dtd : This DTD contains all HTML elements and attributes even deprecated with frameset

8) Protorype in javascript
 Its basically mean of inheritance in javscript
 It allows you to add properties and methods to the existing object
 EX :
  function Pet(name, species, hello)
  {
        this.name = name;
        this.species = species;
        this.hello = hello;
  }
  
  Pet.prototype = {
                              sayHello : function()
                              {
                                  alert(this.hello);
                              }
                           }

9) What does "1"+2+4 evaluate to? What about 5 + 4 + "3"?
124 and 93

10) Variable scoping in javascript
Local scoping : variable in function are availble in function
Global variables are available across the script
Function scope: Nested functions have access to variables declared in their outer scope

 function init() {
          var name = "Mozilla";
          function displayName() {
                  alert(name); <-----------------
          }
         displayName();
 }
 init();

11) What is Javascript Anonymous function
 Anonymous functions are functions that are dynamically declared at runtime
 Example :
  var eatCakeAnon = function(){
          alert("So delicious and moist");
  };
  eatCakeAnon();

  OR

  window.addEventListener("load",   function() { alert("All done");},  false);

12) Hoisting In javascript
Hoisting - Raise at the top
Function declarations and variable declarations are always hoisted(Annonomously defined) invisibly to the top of their containing scope by the JavaScript interpreter.

Within its current scope, regardless of where a variable is declared, it will be, behind the scenes, hoisted to the top.

  var myvar = 'my value'; 
  
 (function() { 
   alert(myvar); // undefined 
   var myvar = 'local value'; 
 })();

 This will return undefined as by functional scope the myvar will be defined at the start of the function invisibly without any value assigned

13) Namespace in javascript
      Its basically an object which contain all you code including property and methods which helps to prevent name clashes with JavaScript code written by others.

 var myApp = {}
 myApp.id = 0;
 myApp.next = function() {
       return this.id++;
 }

 OR self executing function
 var foo = (function(){

            // Create a private variable
            var name = "Spock";
 
           // Create a private function
           var greeting = function() {
                     return "Hello " + name;
            };
 
            // Return an object that exposes our greeting function publicly
            return {
                   greeting: greeting
            };
 })();

14) closure in Javascript
 Closure is local variable of the function which keep alive even after the function have returned or
 its the stack frame which is not deallocated when the function returns
  
   function sayHello(name) {
     var text = 'Hello ' + name;
     var sayAlert = function() { alert(text); }
     sayAlert();
   }
   sayHello('name')
  
 Here function seyHello called which in turn calls seyAlert , even if sayHello have exited the sayAlert will be there in stack n will execute

15) Unobtrusive JavaScript
 Its basically the way with which html and javascript code is kept seperated
 Binding browser events dyamically , creating namespaces for dom element operations

 so instead of
  <input type="text" name="date" onchange="validateDate()" />
 we will write
  <input type="text" name="date" id="date" />
 and
  window.onload = function() {
   document.getElementById('date').onchange = validateDate;
  }
  
16) AJAX
 Asynchromous javascript n xml
 Internally uses xmlhttpobject to make requrest to local server's dynamic pages with http request methods like get,post,put,delete

 Functions open(),send(),abort(),readyState,responseText

 open("GET", url, true); True or false to change to asychronous to synchronous

 readyState = 4 is response complete , 0-3 response in progress from open,setup ,send ,process stages
 Status - http status code 200

 JSONP can be used for crossdomain requests

17) Event Ordering ----- Event Bubbling vs Event Capturing
Event bubbling and capturing are two ways of event propagation in html DOM.
 In bubbling the event is first captured and handled the the inner most element and then  propagated to outer elements.

In capturing the event is first captured by the outer most element and propagated to the inner most element.

 <div>
    <ul>
       <li></li>
    </ul>
 </div>
If you take this structure and assume that a click event has happed in the li element.
In capturing model the event will be handled by the div first(click event handlers in the div will fire first) then in the ul then at the last in the target element li.
In bubbling model it is the opposite. In this model the event will be first handled by the li first and the ul and at the last by the div element.

  element1.addEventListener('click',doSomething2,true)  //capturing
  element2.addEventListener('click',doSomething,false) // Bubbling

18) Getting value of textbox
 document.getElementById('txtbox1').value
 CheckBox : document.getElementById('checkbox1').checked
 Radio : document.getElementsByName(id).lenght parse and check if checked

19) How to handle back button session logout as in banking sites
 window.onbeforeunload = function () { }
 onbeforeunload event will cause the browser to never cache the page: This means that the page will always be rendered exactly as if the user was hitting it for the very first time.

19) Jsonp : http://www.ibm.com/developerworks/library/wa-aj-jsonp1/ 

Comments