Categories
Top 50 JavaScript Interview Questions and Answers
Today, 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 software. 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
1. What is JavaScript?
2. What are the data types supported by JavaScript?
1) Undefined
2) Null
3) Boolean
4) String
5) Symbol
6) Number
7) Object
3. What is the difference between Java & JavaScript?
Java | JavaScript |
Java is an OOPS programming language. | JavaScript is an OOPS scripting language. |
It creates applications that run in a virtual machine or browser. | The code is run on a browser only. |
Java code needs to be compiled. | JavaScript code are all in the form of text. |
4. Is JavaScript a case-sensitive language?
5. What are the features of JavaScript?
2) It is designed for creating network-centric applications.
3) It is complementary to and integrated with Java.
4) It is an open and cross-platform scripting language.
6. What is a name function in JavaScript & how to define it?
function named(){
// write code here
}
7. How can you create an Array in JavaScript?
var x = [];
var y = [1, 2, 3, 4, 5];
8. How can you create an object in JavaScript?
var emp = {
name: "Daniel",
age: 23
};
9. What are the advantages of JavaScript?
Less server interaction −
You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.Immediate feedback to the visitors −
They don’t have to wait for a page reload to see if they have forgotten to enter something.Increased interactivity −
You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.Richer interfaces −
You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.10. Can you assign an anonymous function to a variable and pass it as an argument to another function?
In case you are facing any challenges with these JavaScript Interview Questions, please comment on your problems in the section below.
11. What is Closure? Give an example.
12. What is Callback?
13. What are the scopes of a variable in JavaScript?
Global Variables −
A global variable has global scope which means it is visible everywhere in your JavaScript code.Local Variables −
A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.14. What is the purpose of ‘This’ operator in JavaScript?
15. What is argument objects in JavaScript & how to get the type of arguments passed to a function?
16. How to read a cookie using JavaScript?
The document.cookie string will keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and value is its string value.
You can use strings’ split() function to break the string into key and values.
17. How does Type Of Operator work?
18. What are the variable naming conventions in JavaScript?
1) You should not use any of the JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
2) JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123name is an invalid variable name but _123name or name123 is a valid one.
3) JavaScript variable names are case sensitive. For example, Test and test are two different variables.
19.How to create a cookie using JavaScript?
20. Name some of the built-in methods and the values returned by them.
Built-in Method | Values |
CharAt() | It returns the character at the specified index. |
Concat() | It joins two or more strings. |
forEach() | It calls a function for each element in the array. |
indexOf() | It returns the index within the calling String object of the first occurrence of the specified value. |
length() | It returns the length of the string. |
pop() | It removes the last element from an array and returns that element. |
push() | It adds one or more elements to the end of an array and returns the new length of the array. |
reverse() | It reverses the order of the elements of an array. |
21. What are the ways to define a variable in JavaScript?
Var
The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Example: var a =10; Variable declarations are processed before the execution of the code.Const
The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.Let
It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.22. In how many ways a JavaScript code can be involved in an HTML file?
1) Inline
2) Internal
3) External
An inline function is a JavaScript function, which is assigned to a variable created at runtime. You can differentiate between Inline Functions and Anonymous since an inline function is assigned to a variable and can be easily reused. When you need a JavaScript for a function, you can either have the script integrated in the page you are working on, or you can have it placed in a separate file that you call, when needed. This is the difference between an internal script and an external script.
23. List out the different ways an HTML element can be accessed in a JavaScript code.
getElementById(‘idname’):
Gets an element by its ID namegetElementsByClass(‘classname’):
Gets all the elements that have the given classname.getElementsByTagName(‘tagname’):
Gets all the elements that have the given tag name.querySelector():
This function takes css style selector and returns the first selected element.24. What is the difference between Attributes and Property?
Attributes
provide more details on an element like id, type, value etc.Property
is the value assigned to the property like type=”text”, value=’Name’ etc.25. How to delete a cookie using JavaScript?
Intermediate Level JS Interview Questions and Answers
26. What is the difference between undeclared & undefined?
27. What is the difference between the operators ‘==‘ & ‘===‘?
28. What is the difference between Local storage & Session storage?
Local Storage
The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) – reducing the amount of traffic between client and server. It will stay until it is manually cleared through settings or program.Session Storage
It is similar to local storage; the only difference is while data stored in local storage has no expiration time, data stored in session storage gets cleared when the page session ends. Session Storage will leave when the browser is closed.
In case you are facing any challenges with these JavaScript Interview Questions, please comment on your problems in the section below.
29. What is a Typed language?
Dynamically
in this, the variable can hold multiple types; like in JS a variable can take number, chars.Statically
in this, the variable can hold only one type, like in Java a variable declared of string can take only set of characters and nothing else.30. What is the difference between null & undefined?
31. What is NaN in JavaScript?
In case you are facing any challenges with these JavaScript Interview Questions, please comment on your problems in the section below.
32. What is an event bubbling in JavaScript?
33. What is the difference between innerHTML & innerText?
innerHTML
It will process an HTML tag if found in a stringinnerText
It will not process an HTML tag if found in a string34. Name some of the JavaScript Frameworks
1) Angular
2) React
3) Vue
35. How do JavaScript primitive/object types passed in functions?
By Value
means creating a COPY of the original. Picture it like twins: they are born exactly the same, but the first twin doesn’t lose a leg when the second twin loses his in the war.By Reference
means creating an ALIAS to the original. When your Mom calls you “Pumpkin Pie” although your name is Margaret, this doesn’t suddenly give birth to a clone of yourself: you are still one, but you can be called by these two very different names.36. How can you convert the string of any base to integer in JavaScript?
example:
parseInt("4F", 16)
37. What would be the result of 2+5+”3″?
38. What are Exports & Imports?
Advanced Level JavaScript Interview Questions and Answers for Experienced Professionals
39. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
1) When you use strict mode, you cannot use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible.
2) You can enable strict mode by adding “use strict” at the beginning of a file, a program, or a function.
40. What is a prompt box in JavaScript?
41. How to empty an Array in JavaScript?
Method 1 –
Above code will set the variable arrayList to a new empty array. This is recommended if you don’t have references to the original array arrayList anywhere else, because it will actually create a new, empty array. You should be careful with this method of emptying the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.arrayList = []
Method 2 –
The code above will clear the existing array by setting its length to 0. This way of emptying the array also updates all the reference variables that point to the original array. Therefore, this method is useful when you want to update all reference variables pointing to arrayList.arrayList.length = 0;
Method 3 –
The implementation above will also work perfectly. This way of emptying the array will also update all the references to the original array.arrayList.splice(0, arrayList.length);
Method 4 –
The implementation above can also empty arrays, but it is usually not recommended to use this method often.while(arrayList.length) { arrayList.pop(); }
The implementation above can also empty arrays, but it is usually not recommended to use this method often.
42. What will be the output of the code below?
var Y = 1;
if (function F(){})
{
y += Typeof F;</span>
}
console.log(y);
43. When to Use Internal and External JavaScript Code?
On the other hand, if your JavaScript code is used in many web pages, you should consider keeping your code in a separate file. If you wish to make some changes to your code, you have to change only one file, making code maintenance easy. If your code is too long, it is better to keep it in a separate file. This helps in easy debugging.
44. What are escape characters in JavaScript?
For example-
document.write "I am a "good" boy"
document.write "I am a "good" boy"
45. What is the reason for wrapping the entire content of a JavaScript source file in a function book?
Another feature of this technique is to allow for an easy alias for a global variable. This is often used in jQuery plugins.
46. What are Cookies in JavaScript?
47. What do you mean by Self Invoking Functions?
Normally, we declare a function and call it, however, anonymous functions may be used to run a function automatically when it is described and will not be called again. And there is no name for these kinds of functions.
48. What is the difference between exec () and test () methods in javascript?
We'll use exec () to search a string for a specific pattern, and if it finds it, it'll return the pattern directly; else, it'll return an 'empty' result.
We will use a test () to find a string for a specific pattern. It will return the Boolean value 'true' on finding the given text otherwise, it will return 'false'.
49. What are some advantages of using External JavaScript?
Some advantages of external javascript are
It allows web designers and developers to collaborate on HTML and javascript files.
We can reuse the code.
Code readability is simple in external javascript.