Q) What is JavaScript?

A) It is a dynamic programming language. It is light weight and most commonly used as a part of web pages. It is used to interact with user and make dynamic changes.

Note: In interview you can give below advantages of javascript


Q) What are the advantages of JavaScript?

A) It has less server interaction.
It gives immediate feedback to visitors.
It can provide rich interface.


Q) What are JavaScript limitations?

A) It doesn’t allow reading/writing of files due to security reasons.
It cannot be used for networking applications.
It doesn’t have multi-threading or multi processing capabilities.


Q) Is JavaScript case sensitive?(This is most commonly asked in Interview questions for freshers)

A) Yes. Hence care should be taken while writing variable names and function names.


Q) Give a simple syntax of JavaScript.(The interviewer may ask you to write a simple syntax)

A) Below is the simple syntax for writing Hi.

<Head>
<script type="text/JavaScript">
document.write("Hi");
</script>
</Head>

Q) Write a basic sample of JavaScript function?

A) Below is the basic sample of JavaScript function which writes hello world on button click.

<Head>
<script type="text/javascript">
function Hello()
{
document.write("Hello world");
}
</script>
</Head>

<body>
<input type="button" onClick="Hello()" value="Hello"/>
</body>

Q) What are JavaScript datatypes?

A) JavaScript allows you to work with three primitive data types:

  1. Numbers
  2. Strings
  3. Boolean

Q) Explain JavaScript variables?

A) Variables are nothing but named containers. We can place data in these containers.

“var” keyword is used to declare variables.

Example: var money;
var name;
Note: Javascript is a untyped language. This means that javascript variable can hold a value of any datatype.


Q) What are types of operators in JavaScript?

A) There are many operators which can be broadly classified as follows:
Comparison Operators:

  1. == (To check equal to)
  2. != (Not equal to)
  3. <,>,>=,<= (Less than, greater than, greater than or equal to, less than or equal to)

Logical operators:

  1. && (AND)
  2. || (OR)
  3. ! (NOT)

Conditional(ternary) operators:
?:
If condition is true? then first value; Other wise last value.
Example:

var a=10; b=20;
var result= a>b?a:b;

Note: In above case, it checks if a>b. since a is not greater than b. The result it returns is second value that is b.


Q) What is typeof operator in JavaScript?

A)It evaluate to “number”,”string” or “boolean”
In below example we are checking if a variable is string or an integer:

var a=10;
var b="abc";
result=(typeof a=="string" ? " a is string ":"a is int");

Q) How to redirect a page with JavaScript?

A) We can redirect a page using window.location. Below is the example to redirect to google on button click:

<Head>
<script type="text/javascript">
function Redirect()
{
window.location="https://www.google.com";
}
</script>
</Head>
<body>
<input type="button" onClick="Redirect()"/>
</body>

Q) Explain dialogue boxes in JavaScript.

A) There are basically three types : 1.Alert 2. Confirmation Dialogue box and 3.Prompt dialogue box.

Alert : It is used to show an alert dialogue.
Example:

<script type="text/javascript">
function a()
{
alert("This is Alert");
}
</script>

Confirmation dialogue box : This is used to take confirmation from user.
Example:

<script type="text/javascript">
function a()
{
var bln=Confirm("Are you sure");
if(bln==true)
{
alert("yes");
}
else
{
alert("no");
}
}
</script>

Prompt dialogue box: This is important when you want to show popup a text box to get user input.

Example:

<script type="text/javascript">
function fun()
{
var res=prompt("Enter your name","Your Name");
document.Write("Your name is : "+res);
}
</script>

Q) What is use of window.print()?

A) The Javascript print function window.print() prints the current page when executed.
We can directly call this function using the onclick event.

Example:

<input type="button" onClick="winodw.print()"/>

Q) What are JavaScript arrays? Give example.

A) Array object lets you store multiple values in a single variable. It stores fixed sized sequential collection of elements of same datatype.
We can create array by any of following ways:
var A=new Array(“Apple”,”Art”,”Author”);
or
var A=[“Apple”,”Art”,”Author”]


Q) How to check if JavaScript variable is an array or an object?

A) $.isArray() can be used to check if a variable is an array of an object.(Note: This function supports only new browsers. For old browsers we have to write manual method)


Q) what is let keyword in JavaScript?(As let keyword is new in JavaScript it may be asked in interview)

A) let is similar to var in JavaScript. When var is declared in a block(A block of flower braces{}) in JavaScript. It can be accessed outside the block.
But let declared inside the braces cannot be accessed outside braces. let can be used to redeclaring variable. Below is the difference show of block and redeclare.

{
var x=10;
}
//x is accessible here.
{
let x=10;
}
//x is not accessible here.
var x=10;
{
var x=2;
}
//here x is 2.
var x=10;
{
let x=2;
}
//here x is 10.

Q) How to sort numbers in JavaScript?(This question is tricky hence mostly asked in interview)

A) By default sort order is string in JavaScript.

Consider below example.
var temp={2,13,9,14,10};
temp.sort();
//Here result will be [10,13,14,2,9]

Hence below function can be used to sort the numbers:
temp.sort(function(a,b)
{
return a-b;
});

//Here result will be [2,9,10,13,14]


Q) What is minification?

A) Minification is compressing of JavaScript files which removes extra spaces and renames long variable name with small variable names. This will result in less bandwidth utilization hence increase response time.


Q) What does this refer in JavaScript?

A) This refers to current parent object. If we are in a page then this means a page. If we are in JavaScript function this means window object.


Q) What is the difference between null and undefined?(This is one of interviewers favorite question)

A) Undefined means variable is declared but not assigned, whereas null means variable is declared with null. By default JavaScript variables are undefined.


Q) What is unobtrusive JavaScript?

A) Unobtrusive JavaScript is a JavaScript file that is separated from web sites html markup. The following are benefits of unobtrusive JavaScript.

  • Separation of concerns i.e., html is now clean without any traces of JavaScript
  • Its also easy to update code as all JavaScript logic is present in separate file.

Q) What is the difference between $(document).ready and window.onload?

A) $(document).ready():Its executed first when compared to window.onload. It executes once DOM tree is loaded by browser.

Window.onload():Gets executed when DOM tree is loaded followed by images, flash and external resources, etc.


Q) Name some methods of Arrays in JavaScript.

A) The following are few methods of Arrays in JS:
1. indexof(): Returns the first index of an element within array equal to specified value.

2. pop(): Removes last element from an array and returns that element.

3. push(): Adds one or more elements to the end of array and returns new length of array.


Q) What is Math object in JavaScript?

A)The math object provides methods for mathematical constants and functions.
Below are the methods:
abs(): It is used to make the value absolute
floor(): It returns the floor value
max(): It is used for getting maximum value
min(): It is used for getting minimum value
round(): It is used to round of the given value
ceil(): It returns the ceil value


Q) How to create a JavaScript object and access its properties?

A)You can define and create a JavaScript object with var keyword.Below is the example for same:

var Vehicle={VehicleNo:”AX010″, VehicleName:”MVITO”, VehicleYear:”1997″}

You can access object properties in below ways:

ObjectName.propertyName // (Vehicle.VehicleNo)
or 
objectName["propertyName"] // (Vehicle["VehicleNo"])

Q) Write a simple syntax to iterate an array(The interviewer may ask multiple sample programs)

A)Below is the simple syntax to iterate an array in JavaScript:

var result="";
var numbers=[3,4,5,6];
numbers.forEach(IterateFunction);

function IterateFunction(value,index,array)
{
	result=result+value+"<br>";
}

Q) Explain date formats in JavaScript.

A)There are basically 3 types of JavaScript date input formats:
ISO date: “2017-07-27″(International standard)
Example : var d=new Date(“2017-07-27”);

Short date: “07/27/2017”
Example : var d=new Date(“07-27-2017”);

Long date: “Jul 27 2017”
Example : var d=new Date(“Jul 27 2017”);


Q) Mention a few date methods in JavaScript.

A)Below are few examples of date methods:

getMonth()–Gets month as number–(0 to 11)
getDate()–Gets the day as number–(1 to 31)
getTime()–Gets the time (It will get milliseconds since Jan 1 1970)
getDay()–Gets the weekday as number(0 to 6)


Q) How to find type of data in JavaScript?(This is important interview question and used mostly while developing applications)

A)You can use the typeof operator to find the data type of a JavaScript variable:
Example:

typeof "MAK"//Returns "string"
typeof 3.45 //Returns "number"
typeof false//Returns "boolean"
typeof newVehicle//Returns "undefined"
typeof [14,24,74,84]//Returns object

(Note: There are basically six types of objects which include Object, Date, Array, String, Number and Boolean)


Q) How to convert string to number and vice versa in JavaScript?

A)Below syntax can be used to convert from number to strings:

String(123);//Converts from number to string
(123).toString();//Converts from number to string

Below syntax converts from string to number:(Note: If number is unable to convert string to number it will return NaN)

Number("3.45")//returns number 3.45

Q) Explain with an example how can we handle JavaScript exceptions?

A)Exceptions can be handled with try and catch statements in JavaScript. Below is the example which show catching of exception when alert is wrongly spelt as allert.

<p id="test"></p>

<script>
try{
allert("Hellow world");
}
catch(err)
{
document.getElementById("test").innerHTML=err.message;
}
</script>

Q) Explain JavaScript Hoisting

A)JavaScript declarations are hoisted but initializations are not. For example a variable can be used before it is declared but it cannot be used if its declare and initialized.

Below is the example for same:


x=5;

alert(x);

var x;
//It runs smoothly without any errors

alert(x);

var x=5;
//Gives x as undefine