Here I have listed most commonly asked JavaScript interview questions and answers for job seekers to by employers to test JavaScript Knowledge. I will constantly be updating this blog and adding more questions to the list.

What is JavaScript ?
JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages.The general-purpose core of the language has been embedded in Netscape, Internet Explorer,and other web browsers.

Difference between JavaScript and JAVA

It’s important to understand the difference between Java and JavaScript. Java is a full programming language developed by Sun Microsystems with formal structures, etc. JavaScript is a scripting language developed by Netscape that is used to modify web pages. Most JavaScript must be written in the HTML document between <SCRIPT> tags. You open with a <SCRIPT> tag, write your JavaScript, and write a closing </SCRIPT> tag. Sometimes, as an attribute to script, you may add Language=JavaScript” because there are other scripting languages as well as JavaScript that can be used in HTML
Javascript has long been the subject of many heated debates about whether it is possible to use it while still adhering to best practices regarding accessibility and standards compliance. The answer to this question is still unresolved, however, the emergence of Javascript frameworks like jQuery has provided the necessary tools to create beautiful websites without having to worry(as much) about accessibility issues. Obviously there are cases where a Javascript solution is not the best option. The rule of thumb here is: use DOM scripting to enhance functionality, not create it.

Here is a list of Most Common JavaScript Interview Questions with Answers for Practice

ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.
Number, String, Boolean, Function, Object, Null,Undefined.
Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F",16);
Return true if the argument is not a number.

It’s a number in JavaScript, derived by dividing negative number by zero.

Since 1 is a string, everything is a string, so the result is 124.

Since 2 and 5 are integers, this is number arithmetic,since 8 is a string, it’s concatenation, so 78 is the result

for, while, do-while loops, but no foreach.

- var obj = new Object(); or var obj = {};

obj["age"] = 17 or obj.age = 17

It refers to the current object.

Following are the features of JavaScript −
  •  JavaScript is a lightweight, interpreted programming language.
  • JavaScript is designed for creating network-centric applications.
  • JavaScript is complementary to and integrated with Java.
  • JavaScript is is complementary to and integrated with HTML.
  • JavaScript is open and cross-platform.
Following are the advantages of using JavaScript −
 
Less server interactionYou 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 interfacesYou can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors

We can not treat JavaScript as a full fledged programming language. It lacks the following important features −

Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reason.

JavaScript can not be used for Networking applications because there is no such support available.

JavaScript doesn't have any multithreading or multiprocess capabilities.

Yes! JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

JavaScript supports Object concept very well. You can create an object using the object literal as follows −

var emp = {
    name: "Zara",
    age: 10
};

You can write and read properties of an object using the dot notation as follows −

// Getting object properties

emp.name // ==> Zara
emp.age // ==> 10

// Setting object properties

emp.name = "Daisy" //
<== Daisy
emp.age = 20 // <== 20

You can define arrays using the array literal as follows −

var x = [];

var y = [1, 2, 3, 4, 5];

An array has a length property that is useful for iteration. We can read elements of an array as follows −

var x = [1, 2, 3, 4, 5];

for (var i = 0; i < x.length; i++) {

// Do something with x[i]

}

A named function has a name when it is defined. A named function can be defined using function keyword as follows −

function named() {

// do some stuff here

}

A function in JavaScript can be either named or anonymous.

An anonymous function can be defined in similar way as a normal function but it would not have any name.

Yes! An anonymous function can be assigned to a variable.

JavaScript variable arguments represents the arguments passed to a function.


Yes! An anonymous function can be passed as an argument to another function.

Using typeof operator, we can get the type of arguments passed to a function. For example -

function func(x) {
      console.log(typeof x, arguments.length);
}
func(); //==> "undefined", 0
func(1); //==> "number", 1
func("1", "2", "3"); //==> "string", 3

Using arguments.length property, we can get the total number of arguments passed to a function. For example −

function func(x){ 
	console.log(typeof x, arguments.length); 
} 
func(); //==> "undefined", 0 
func(1); //==> "number", 1 
func("1", "2", "3"); //==> "string", 3
How can you get the total number of arguments passed to a function?

The arguments object has a callee property, which refers to the function you're inside of. For example −

function func() { 
	return arguments.callee; 
} 
func(); // ==> func
How can you get the reference of a caller function inside a function?

JavaScript famous keyword this always refers to the current context

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 - local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

A local variable takes precedence over a global variable with the same name.

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −

function create() { 
	var counter = 0; 
	return { 
		increment: function() { 
			counter++; 
		}, 
		
		print: function() { 
			console.log(counter); 
		} 
	} 
} 
var c = create(); 
c.increment(); 
c.print(); // ==> 1
Give an example of closure?

charAt() method returns the character at the specified index.

concat() method returns the character at the specified index.

forEach() method calls a function for each element in the array.

indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.

length() method returns the length of the string.

pop() method removes the last element from an array and returns that element.

push() method adds one or more elements to the end of an array and returns the new length of the array.

reverse() method reverses the order of the elements of an array −− the first becomes the last, and the last becomes the first.

sort() method sorts the elements of an array.

substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

toLowerCase() method returns the calling string value converted to lower case.

toUpperCase() method returns the calling string value converted to upper case.

toString() method returns the string representation of the number's value.

While naming your variables in JavaScript keep following rules in mind.

You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.

JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

JavaScript variable names are case sensitive. For example, Name and name are two different variables.

The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.

The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.

The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this −

Syntax −

Here expires attribute is option. If you provide this attribute with a valid date or time then cookie will expire at the given date or time and after that cookies' value will not be accessible.

Reading a cookie is just as simple as writing one, because the value of the document.cookie object is the cookie. So you can use this string whenever you want to access the cookie.

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.


Sometimes you will want to delete a cookie so that subsequent attempts to read the cookie return nothing. To do this, you just need to set the expiration date to a time in the past.

document.cookie = "key1 = value1; key2 = value2; expires = date";
How to create a Cookie using JavaScript?

his is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows −

<head>
<script type="text/javascript"> 
	<!-- 
	window.location="http://www.newlocation.com"; 
//--> 
</script> </head>
How to redirect a url using JavaScript?

JavaScript helps you to implement this functionality using print function of window object. The JavaScript print function window.print() will print the current web page when executed.

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ).

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.

You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.

The onerror event handler was the first feature to facilitate error handling for JavaScript. The error event is fired on the window object whenever an exception occurs on the page.

The onerror event handler provides three pieces of information to identify the exact nature of the error −

Error message − The same message that the browser would display for the given error.

URL − The file in which the error occurred.

Line number − The line number in the given URL that caused the error.

Also Read :

http://techhowdy.com/wp-content/uploads/2017/12/Sample-JavaScript-Interview-Questions-and-Answers.pnghttp://techhowdy.com/wp-content/uploads/2017/12/Sample-JavaScript-Interview-Questions-and-Answers-150x150.pngDemonInterview QuestionsSample JavaScript Interview Questions and AnswersHere I have listed most commonly asked JavaScript interview questions and answers for job seekers to by employers to test JavaScript Knowledge. I will constantly be updating this blog and adding more questions to the list.What is JavaScript ? JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you...Latest technology news