Javascript



21. What is a named function in JavaScript? How to define a named function?  

A named function has a name when it is defined. A named function can be defined using function keyword as follows -

           function named(){
           // do some stuff here
           }
           


22. How will you get the Checkbox status whether it is checked or not?  

              var status = document.getElementById('checkbox1').checked; 
              alert(status); 
              
will return true or false.


23. What is a prompt box?  

A prompt box is a box which allows the user to enter input by providing a text box. Label and box will be provided to enter the text or number.



24. How to convert JSON Object to String?  

         var myobject=['Web','Technology','Experts','Notes']
         JSON.stringify(myobject);   
         



25. How to convert JSON String to Object?  

 
             var jsonData = '{"name":"web technology","year":2015}';
             var myobject = JSON.parse(jsonData);
             console.log(myobject);  
             


26. How to check an variable is Object OR String OR Array?  

Use below function to get Data type of javascript variable.
              function checkDataType(someVar){
               result ='String';
                  if(someVar instanceof Object){
                     result ='Object'
                  }
                  if($.isArray(someVar)){
                    result = 'Array';
                  }
             return result;
             }

             var someVar= new Array("Saab", "Volvo", "BMW");
             console.log(result); 
             


27. How many types of functions JavaScript supports?  

A function in JavaScript can be either named or anonymous.



28. How to define a anonymous function?  

An anonymous function can be defined in similar way as a normal function but it would not have any name.



29. Can you assign a anonymous function to a variable?Can you pass a anonymous function as an argument to another function?  

Yes! An anonymous function can be assigned to a variable.

Yes! An anonymous function can be passed as an argument to another function.



30. What do you understand by this keyword in JavaScript?  

In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context that is placed on the stack. The following gives two different results (in the browser, where by-default the window object is the 0-level context):
             var obj = { outerWidth : 20 };
 
             function say() {
                  alert(this.outerWidth);
             }
 
             say();//will alert window.outerWidth
             say.apply(obj);//will alert obj.outerWidth
             


31. What does "1"+2+4 evaluate to?  

Since 1 is a string, everything is a string, so the result is 124.



32. What does 3+4+"7" evaluate to?  

Since 3 and 4 are integers, this is number arithmetic, since 7 is a string, it is concatenation, so 77 is the result.



33. How can you get the type of arguments passed to a function?  

Using typeof operator, we can get the type of arguments passed to a function. For example -
              function func(x){
                  console.log(typeof x, arguments.length);
              }
              func();                //==> "undefined", 0
              func(1);               //==> "number", 1
              func("1", "2", "3");   //==> "string", 3
              


34. What are the valid scopes of a variable in JavaScript?  

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

(i)Global Variables - A global variable has global scope which means it is visible everywhere in your JavaScript code.
(ii)Local Variables - A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.



35. What is callback?  

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.



36. What looping structures are there in JavaScript?  

Three types of looping structures are there in JavaScript
for,while,do-while loops.



37. Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?  

Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.

The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs the timer to stop.

Timers are operated within a single thread, and thus events might queue up, waiting to be executed.



38. Which symbol is used for comments in Javascript?  

// for Single line comments and

/* Multi

Line

Comment

*/



39. What is difference between undefined and object?  

undefined means some variable's value is not defined yet.
object means variables's value is defined that is either function, object OR array.

With use of below, you can easily determine whether it is object OR NULL.

                  console.log(typeof(null));      // object
                  console.log(typeof(undefined)); // undefined  
                  


40. What is the difference between ViewState and SessionState?  

'ViewState' is specific to a page in a session.

'SessionState' is specific to user specific data that can be accessed across all pages in the web application.



Java Interview Question

.Net Interview Question

PHP Interview Question

AngularJS Interview Questions