JavaScript Interview Questions
-
1 What is JavaScript
JavaScript gives you superpowers. The true computer programming language of the web, JavaScript lets
you add behavior to your web pages. It is lightweight and most commonly used as a part of web pages.
it’s also supported in all modern (and most ancient) browsers. -
2 What is Variables and values in JavaScript
JavaScript statements usually involve variables. Variables are used to store values. We always start with the var keyword when declaring a variable.
Here are a few examples:
var winners = 2; //This statement declares a variable named winners and assigns a numeric value of 2 to it.
var name = "Duke"; //This one assigns a string of characters to the variable name (we call those "strings",for short).
var isEligible = false; //And this statement assigns the value false to the variable isEligible. We call true/false values "booleans".
-
3 What is the difference between == and === in JavaScript.
Using the == operator (Equality)
Here the == operator is checking the values of the two objects and returning true
true == 1; //true, because 'true' is converted to 1 and then compared
"2" == 2; //true, because "2" is converted to 2 and then compared
=== means "exactly equal value and equal type"
Here the === is seeing that they're not the same type and returning false
true === 1; //false
"2" === 2; //false -
4 Explain about Data Types in JavaScript.
JavaScript variables can hold many data types. According to the latest ECMAScript release, these are the
data types:
Boolean
Null
Undefined
Number
String
ObjectBoolean :
Booleans are true and false. They're often used for conditional statements
var raining = false;
if(raining) {
bringUmbrella();
}Number
JavaScript has only one type of number. Numbers can be written with or without decimals.
var length = 25;String
JavaScript strings are used for storing and manipulating text.
var lastName = "Letsknowit";Object:
Objects are variables too. But objects can contain many values
In real life, a Bus is an object.
A Bus has properties like weight and color, and methods like start and stop:All Bus have the same properties, but the property values differ from car to car.
All cars have the same methods, but the methods are performed at different times.
var x = {firstName:"Thomas", lastName:"Cook"}; // ObjectUndefined:
A variable without a value, variable has been declared but has not yet been assigned a value. The typeof
is also undefined.
var Bus;
alert(Bus); //shows undefined
alert(Bus); //shows undefinedNull:
In JavaScript null is "nothing". It is supposed to be something that doesn't exist, but type is still an object
var person = {firstName:"Thomas", lastName:"Cook", age:50, eyeColor:"blue"};
person = null; // Now value is null, but type is still an object
Note: Undefined and null are equal in value but different in type: -
5 What is string in JavaScript.
A string can be any text inside double or single quotes.
var demo = "Bullet110";
var audi = 'Audi XC60'.
Note: You can use single or double quotes: -
6 What is difference between local and global variable
If a variable is declared outside a function, it’s GLOBAL. If it’s declared inside a function, it’s LOCAL
Local Variable
Local Variables that is declared inside a function definition is called local and has scope to that function
only. Variables with the same name can be used in different functions$(document).ready(function(){
function myFunction() {
// A local variable is declared in this function.
var text = "Hello World !";
alert("Value of 'a' inside the function " + a); //Hello World !
}
})Global Variable
A variable that is declared outside of a function definition that is called a global variable and its scope is
throughout your program means all scripts and functions on a web page can access it.
<script >
$(document).ready(function(){// Global variable.
var vGlobal = "Hello World";
// code here can use vGlobal
function myFunction() {
// code here can also use carName
}}
});</script>
-
7 What is DOM in JavaScript
DOM is Document Object Modal. It defines the logical structure of documents and the way a
document is accessed and manipulated. DOM created when the browser loads your page.Note: When you load a page into the browser, not only does the browser parse the HTML and then
render it to the display, it also creates a set of objects that represent your markup. These objects are
stored in the DOM. -
8 What is the difference between function and object
Function:
JavaScript function is a block of code that performs a task or calculates a value. To use a function, you
must define it somewhere in the scope from which you wish to call it.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses
().
function name(parameter1, parameter2, parameter3) {
code to be executed
}Objects:
A JavaScript objects is a collection of properties, and a property is an association between a name (or
key) and a value. Objects can contain many values.
var bus = {type:"Fiat", model:"500", color:"white"};
Bus is an object. A bus has properties like modal, weight and color -
9 How to compare null and undefined
Comparing these values evalutates to true. That might seem odd as well, but it’s the rule.For some
insight, these values both essentially represent "no value"(that is, a variable with no value, or an object
with no value), so they are considered to be equal.
undefined == null //it returns TURE -
10 Explain type of object in JavaScript
In JavaScript, almost "everything" is an object.
In JavaScript there are 5 different data types that can contain values:
string : Strings can be objects (if defined with the new keyword)
number : Numbers can be objects (if defined with the new keyword)
boolean : Booleans can be objects (if defined with the new keyword)
object : Objects are always objects
function : An object method is an object property containing a function definition.
There are 3 types of objects:
Object : Objects are always objects
Date: Dates are always objects
Array: Array are always objects -
11 Explain JavaScript Function Scope
In JavaScript there are two types of scope:
Local scope : A variable declared inside a function, becomes LOCAL They can only be accessed from
within the function.Note: Variables defined inside a function are not accessible (visible) from outside the function.
Global scope : Any variable declared outside of a function, belongs to the GLOBAL scope, and is therefore accessible by all scripts and functions on a web anywhere in your code. -
12 What is array in JavaScript and how can we define array in JavaScript
The Array object store multiple values in a single variable and is used to store a collection of data
Syntax:
An array is a special variable, which can hold more than one value at a time.
var array_name = [item1, item2, ...];
var flavors = ["vanilla", "butterscotch", "lavender", "chocolate", "cookie dough"];
Note: When you create an array, each array item is placed at a location, or index, in the array. With the
flavors array, the first item, "vanilla", is at index 0, the second, "butterscotch" is at index 1, and so on. -
13 What is the difference between window.load and document.ready in JavaScript
document.ready will execute right after the HTML document is loaded property, and the DOM is ready.
The window.load however will wait for the page to be fully loaded, this includes inner frames, images
etc. -
14 What is post- increment and pre- increment in JavaScript
The difference between i++ and ++i is the value of the expression.
Post-Increment: In the first case (i.e. post-increment) the operator increases the variable var1 by 1 but
returns the value before incrementing.
var i = 42;
alert(i++); // shows 42
Pre-Increment: In the second case (i.e. pre-increment) the operator increases the variable var1 by 1 but
returns the value after incrementing.
var i = 42;
alert(++i); // shows 43 -
15 What is boolean expression in JavaScript
A JavaScript Boolean represents one of two values: true or false.
Boolean(10 > 9) // returns true -
16 How to write while loop statment in JavaScript
<script>
$(document).ready(function(){
var icecream = 5;
while (icecream > 0) {
console.log("Another scoop!");
icecream = icecream - 1;
}console.log("Life without ice cream isn't the same");
});
</script> -
17 What are Callback functions in JavaScript
A callback function is executed after the current effect is done, Callback
event handler.
also known as an
Example
$(document).ready(function(){
$("button").click(function(){
$("div").hide("slow", function(){
alert("The div is now hidden"); //this will notify you after successfully finish the hide effect
});
});
});
Note: Callback function will prevent your code if previous effect is not finished successfully and stop
executing the next line. -
18 What is prototype in JavaScript
The JavaScript prototype property also allows you to add new properties and methods to objects
constructors:
Using the prototype Property. The JavaScript prototype property allows you to add new properties to
object constructors:<script>
function Dog(name, breed, weight) {
this.name = name;
this.breed = breed;this.weight = weight;
this.color = "Red";
}
Dog.prototype.color = "Red";
var myDog =
new Dog ("tommy", "mixed", 50);
console.log(myDog );
</script> -
19 What are falsey value in JavaScript
There are five falsey values in JavaScript:
undefined is falsey.
null is falsey.
0 is falsey.
The empty string is falsey.
NaN is falsey. -
20 How to create chaining in JavaScript
Chaining is really just a shorthand for a longer series of steps to access properties and
methods of objects (and arrays). Let’s take a closer look at what we just did to combine
two statements with chaining.
var ship = { locations: ["06", "16", "26"], hits: ["", "", ""] }; //Here's a ship object.
var locations = ship.locations; //We were grabbing the locations array from the ship
var index = locations.indexOf(guess); And then using it to access the indexOf method.
We can combine the bottom two statements by chaining together the expressions (and getting rid of the
variable locations):ship.locations.indexOf(guess)
1. ship Evaluates to the ship object.
2. location which has a locations property, which is an array.
3. indexOf Which has a method named indexOf. -
21 What is Comparison Operators in JavaScript
Comparison operators are used in logical statements to determine equality or difference between
variables or values and result in true or false. For example, we can use the boolean comparison operator
< ( less than ) like this: 3 < 6. This expression results in true -
22 How may types of operators in JavaScript
There are 2 types of operators:
Comparison Operators
Comparison operators compare two values. Here are some commoncomparison operators:
< means less than
> means greater than
== means equal to
=== means exactly equal to (we’ll come back to this one later!)
<= means less than or equal to
>= means greater than or equal to
!= means not equal to
Logical Operators
Logical operators combine two boolean expressions to create one boolean result (true or false). Here
are two logical operators:
|| means OR. Results in true if either of the two expressions is true.
&& means AND. Results in true if both of the two expressions are true
Please rotate your device
We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site