JavaScript was created by Brendan Eich in 1995. Initially designed to interact with elements of web pages. Today it has far more powerful uses and companies like Google and Facebook use JavaScript to build complex desktop-like web applications. With the launch of Node.js, It has also become one of the most popular languages for building server-side applications. Today, even the web isn’t big enough to contain JavaScript’s versatility. I believe that you are already aware of these facts and this has made you land on this JavaScript Interview Questions article.
So, if you are planning to start your career in JavaScript and you wish to know the skills related to it, now is the right time to dive in, when the technology is in its blossoming state. JavaScript Interview Questions will provide you with in-depth knowledge and help you prepare for your interviews.
1. What is JavaScript?
JavaScript is a client-side as well as server-side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object based Programming language.
2. Which company developed JavaScript?
Netscape is the software company who developed JavaScript.
3. How are JavaScript and ECMA Script related?
ECMA Script are like rules and guideline while JavaScript is a scripting language used for web development.
4. How to write comments in javaScript?
JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript supports two ways comments
- Single Line Comment
- Multi-Line Comment
// Single line comment in javaScript
console.log("Hello JavaScript");
/*
JavaScript Supports
Multiline Comments also
To make it more readable
*/
5. What are JavaScript Data Types?
Following are the JavaScript Data types:
- Number
- String
- Boolean
- Object
- Undefined
- Symbol (ES6)
6. What is the use of isNaN function?
isNan function returns true if the argument is not a number otherwise it is false.
7. What are undeclared and undefined variables?
Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.
8. What are global variables? How are these variable declared and what are the problems associated with using them?
Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.
// Declare a global
globalVariable = "Hello World";
The problems that are faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.
9. What is ‘this’ keyword in JavaScript?
‘this’ keyword refers to the object from where it was called.
10. Explain the working of timers in JavaScript? Also explain 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.
11. What is === operator?
=== is called as strict equality operator or identity operator which returns true when the two operands are having the same value without any type conversion.
12. Explain how can you submit a form using JavaScript?
To submit a form using JavaScript use document.form[0].submit();
// Submit form using JavaScript
document.form[0].submit();
13. How can the style/class of an element be changed?
It can be done in the following way:
document.getElementById("textid").style.fontSize = "20px";
// or
document.getElementById("textid").className = "classname";
14. What are all the looping structures in JavaScript?
Following are looping structures in JavaScript:
- for
- while
- do-while
15. Explain the difference between “==” and “===”?
“==” 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.
16. What would be the result of 3+2+”7″?
Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will be done. So the result would be 57.
17. Explain how to detect the operating system on the client machine?
In order to detect the operating system on the client machine, the navigator.platform string (property) should be used.
18. What do mean by null in JavaScript?
The null value is used to represent no value or no object. It implies no object or null string, no valid boolean value, no number and no array object.
19. What is the function of delete operator?
The delete keyword is used to delete the property as well as its value.
var employee = {age:30, name:"John"};
delete employee.age
20. What is an undefined value in JavaScript?
Undefined value means the
- Variable used in the code doesn’t exist
- Variable is not assigned to any value
- Property doesn’t exist
21. What is the use of Void(0)?
- 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.
22. What are the two basic groups of dataTypes in JavaScript?
In JavaScript, a variable may store two types of values: primitive and reference.
JavaScript provides six primitive types and one reference type.
Primitive | Reference |
undefined | Object |
null | |
Boolean | |
Number | |
String | |
Symbol (ES6) |
Primitive value: when you access the variable, you manipulate the actual value stored in that variable. In other words, the variable that stores a primitive value is accessed by value.
Reference: when you manipulate an object, you work on the reference of that object, rather than the actual object. It means a variable that stores an object is accessed by reference.
23. What is the use of typeof operator?
typeof is an operator which is used to return a string description of the type of a variable.
24. Which keywords are used to handle exceptions?
try…catch…finally statement is used to handle exceptions in the JavaScript
try {
//tryCode - Block of code to try
}
catch(err) {
//catchCode - Block of code to handle errors
}
finally {
//finallyCode - Block of code to be executed regardless of the try / catch result
}
25. Which keyword is used to print the text in the screen?
If we want to print the text “Welcome to the TechBowl”. we can print this text like this.
document.write("Welcome to the TechBowl")
26. What are the different types of errors in JavaScript?
There are three types of errors:
Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.
Run time errors: Errors that come due to misuse of the command inside the HTML language.
Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having different operation.
27. What is the difference between JavaScript and Jscript?
Both are almost similar. JavaScript is developed by Netscape and Jscript was developed by Microsoft.
28. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
Strict Mode adds certain compulsions to JavaScript. Under the strict mode, JavaScript shows errors for a piece of codes, which did not show an error before, but might be problematic and potentially unsafe. Strict mode also solves some mistakes that hamper the JavaScript engines to work efficiently.
Strict mode can be enabled by adding the string literal “use strict” above the file. This can be illustrated by the given example:
function myjsFunction() {
"use strict";
var val = "This is a strict mode function";
}
29. What is the role of break and continue statements?
Break statement is used to come out of the current loop while the continue statement continues the current loop with a new recurrence.