Java Script

Interview Questions on Java Script

21. How to convert number to string?

var num = 15;
var n = num.toString();

22. What is the difference b/w == and ===?

==  compares the values.
=== compare values and its type.

23. How to create an array in javascript?

Method1 :- var cars = ["Saab", "Volvo", "BMW"];
Method2 :-  var cars = new Array("Saab", "Volvo", "BMW");

24. How to declare and initialize object?

 Syn :- var objectname = {keyname1:vaule1, keyname2:value2};
 Ex :- var person = {firstName:"John", lastName:"Doe", age:46};

25. How to know the type of vaue of variable?

you can use the JavaScript typeof operator to find the type of a JavaScript variable
Ex :- 
typeof "John"                // Returns string 
typeof 3.14                  // Returns number
typeof false                 // Returns boolean
typeof [1,2,3,4]             // Returns object
typeof {name:'John', age:34} // Returns object

26. If we try to print variable which is not initialize what will be the output?


27. What is the type of null variable?


28. What is the type of undefined variable?


29. What is the difference b/w undefined and null?

Type of undefined is undefined.
Type of null is object.

30. What is the difference b/w empty and null?

Empty is a value.
Null is not a value.