1. What is JavaScript?
Ans: JavaScript is a client-side scripting language now days we can works on server side. It works mostly with html but now it can be used without html.
2. Differences between Java and JavaScript?
Ans: Java is complete programming language. JavaScript is a scripting language. So the main differences are java use the compiler whereas JavaScript use interpreter. Second Java is based on OOPS concept whereas JavaScript based on functional programming concept.
3. What are JavaScript types?
Ans: In JavaScript we can use
a. Number
b. String
c. Boolean
d. Function
e. Object
f. Null
g. Undefined
4. What is the use of isNaN function?
Ans: Return true if the argument is not a number otherwise it will return false.
Ex –
console.log(isNaN("hi")); // true
console.log(isNaN(123)); // false
//Some spacial case -
console.log(isNaN({})); // true
console.log(isNaN([])); // false
5. What is negative infinity?
Ans: It’s a special number and it can be derived by dividing a negative number by zero.
6. Which symbol is used for comments in Javascript?
Ans:
// for Single line comments and
/*
Multi Line Comment
*/
7. What is the use of Void(0)?
Ans: Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.
Void(0) is used to call another method without refreshing the page.
8. Difference between “==” and “===”?
Ans: “==” checks only for equality in value whereas “===” is a stricter equality test and returns false if either the value or the type of the two variables are different.
9. What would be the result of 1+2+”3″?
Ans: “33”, because addition precedence works left to right so 1+2 will add first then 3+”3″ will concat;
10. What would be the result of “3”+2+1?
Ans: “321”, because addition precedence works left to right so “3”+2 will concat first then “32”+1 will concat;
11. How to create Array?
Ans: var arr = [1, 2, 3];
12. How to create Object?
Ans: var obj = { msg: “Welcome” };
13. How to empty an array in JavaScript?
Ans:
var arr = ['a','b','c'];
Method 1- arr = [];
Method 2- arr.length = 0;
Method 3- arr.splice(0, arr.length);
14. How do you check if an object is an array or not?
Ans:
var arr = [1, 2, 3];
function isArray (arr) {
return Object.prototype.toString.call(arr).indexOf("Array") != -1;
}
15. What typeof returns for a null value?
Ans: It will returns “object”.
16. What will be the output?
(function(){
var a = b = 3;
})();
console.log("a defined? " + (typeof a !== 'undefined'));
console.log("b defined? " + (typeof b !== 'undefined'));
Ans:
"a defined?" false
"b defined?" true
Because this code work like
b = 3;
var a = b;
so that’s way b work as a globe variable and accessible outside the function.
17. What will be the output?
console.log(false == '0')
console.log(false === '0')
Ans:
console.log(false == '0'); // true
console.log(false === '0') // false
Because === checks type as well as equality but == only checks equality.
18. How to convert JSON Object to String?
Ans:
var langcoll = ['c','c++','java','php']
JSON.stringify(langcoll); // "["c","c++","java","php"]"
19. Differences between onclick and onchange event?
Ans: onclick event triggered when user click on element. Mostly it’s used on buttons.
onchange event triggered when the value of an element has been changed. Mostly it’s used on radio and checkbox, the onchange event occurs when the checked state has been changed.
20. Differences between keydown and keypress event?
Ans: keydown triggered when key pressed down and keypress triggered when key pressed down which is similar to key down but the main differences are it’s not fired for some special key like (e.g. ALT, CTRL, SHIFT, ESC).
Here you can prepare jquery interview questions.