JavaScript Interview Questions
JavaScript Interview Questions
Java JavaScript
Java code needs to be compiled. JavaScript code are all in the form of text.
• Undefined
• Null
• Boolean
• String
• Symbol
• Number
• Object
Q4. What are the features 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.
1. var emp = {
2. name: "Daniel",
3. age: 23
4. };
1. var x = [];
2. var y = [1, 2, 3, 4, 5];
1. function named(){
2. // write code here
3. }
Q10. Can you assign an anonymous function to a variable and pass it as an argument
to another function?
Yes! An anonymous function can be assigned to a variable. It can also be passed 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.
Q11. What is argument objects in JavaScript & how to get the type of arguments
passed to a function?
JavaScript variable arguments represents the arguments that are passed to a
function. Using typeof operator, we can get the type of arguments passed to a function.
For example:
1. function func(x){
2. console.log(typeof x, arguments.length);
3. }
4. func(); //==> "undefined", 0
Q12. What are the 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.
• 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.
Q16. Name some of the built-in methods and the values returned by them.
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.
Syntax :
1 document.cookie = "key1 = value1; key2 = value2; expires = date";
• 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.
Property- is the value assigned to the property like type=”text”, value=’Name’ etc.
Q23. List out the different ways an HTML element can be accessed in a JavaScript
code.
Here are the list of ways an HTML element can be accessed in a Javascript code:
(i) getElementById(‘idname’): Gets an element by its ID name
(ii) getElementsByClass(‘classname’): Gets all the elements that have the given
classname.
(iii) getElementsByTagName(‘tagname’): Gets all the elements that have the given tag
name.
(iv) querySelector(): This function takes css style selector and returns the first selected
element.
Q24. In how many ways a JavaScript code can be involved in an HTML file?
There are 3 different ways in which a JavaScript code can be involved in an HTML file:
• Inline
• Internal
• External
• 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.
Q26. What is a Typed language?
Typed Language is in which the values are associated with values and not
with variables. It is of two types:
• 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.
o
Q27. 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.
Q28. What is the difference between the operators ‘==‘ & ‘===‘?
The main difference between “==” and “===” operator is that formerly compares variable
by making type correction e.g. if you compare a number with a string with numeric literal,
== allows that, but === doesn’t allow that, because it not only checks the value but also
type of two variable, if two variables are not of the same type “===” return false, while
“==” return true.
A JavaScript
framework is an application framework written in JavaScript. It differs from a JavaScript
library in its control flow. There are many JavaScript Frameworks available but some of
the most commonly used frameworks are:
• Angular
• React
• Vue
JavaScript window is a global object which holds The document also comes under the window and ca
variables, functions, history, location. be considered as the property of the window.
In case you are facing any challenges with these JavaScript Interview Questions,
please comment on your problems in the section below.
• 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.
Q37. How can you convert the string of any base to integer in JavaScript?
The parseInt() function is used to convert numbers between different bases. It takes the
string to be converted as its first parameter, and the second parameter is the base of the
given string.
For example
1. parseInt("4F", 16)
• 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.
• You can enable strict mode by adding “use strict” at the beginning of a file, a program, or
a function.
1. var Y = 1;
2. if (function F(){})
3. {
4. y += Typeof F;</span>
5. }
6. console.log(y);
The output would be 1undefined. The if condition statement evaluates using eval, so
eval(function f(){}) returns function f(){} (which is true). Therefore, inside the if statement,
executing typeof f returns undefined because the if statement code executes at run time,
and the statement inside the if condition is evaluated during run time.
Syntax-
The apply() method calls a function with a given this value, and arguments provided as
an array.
Syntax-
1. fun.apply(thisArg, [argsArray])
Method 1 –
1. arrayList = []
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.
Method 2 –
1. arrayList.length = 0;
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.
Method 3 –
1. arrayList.splice(0, arrayList.length);
The implementation above will also work perfectly. This way of emptying the array will
also update all the references to the original array.
Method 4 –
1. while(arrayList.length)
2. {
3. arrayList.pop();
4. }
The implementation above can also empty arrays, but it is usually not recommended to
use this method often.
The output would be 0. The delete operator is used to delete properties from an object.
Here x is not an object but a local variable. delete operators don’t affect local variables.
In case you are facing any challenges with these JavaScript Interview Questions,
please comment on your problems in the section below.
The output would be undefined. The delete operator is used to delete the property of an
object. Here, x is an object which has the property foo, and as it is a self-invoking function,
we will delete the foo property from object x. After doing so, when we try to reference a
deleted property foo, the result is undefined.
Q47. What will be the output of the following code?
1. var Employee =
2. {
3. company: 'xyz'
4. }
5. var Emp1 = Object.create(employee);
6. delete Emp1.company Console.log(emp1.company);
The output would be xyz. Here, emp1 object has company as its prototype property. The
delete operator doesn’t delete prototype property. emp1 object doesn’t have company as
its own property. However, we can delete the company property directly from the
Employee object using delete Employee.company.
The output would be Reference Error. A function definition can have only one reference
variable as its function name.
Q49. What is the reason for wrapping the entire content of a JavaScript source file in a
function book?
This is an increasingly common practice, employed by many popular JavaScript libraries.
This technique creates a closure around the entire contents of the file which, perhaps
most importantly, creates a private namespace and thereby helps avoid potential name
clashes between different JavaScript modules and libraries.
Another feature of this technique is to allow for an easy alias for a global variable. This is
often used in jQuery plugins.
For example-