Javascript Interview Questions & Answers

  • Home
  • Interview_Question

Interview Questions & Answers



Select Series



How can you create an Object in JavaScript?

JavaScript supports Object concept very well. You can create an object using the object literal as follows − var emp = { name: "Zara", age: 10 };

How many types of functions JavaScript supports?

A function in JavaScript can be either named or anonymous.

How to define a anonymous function?

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

Can you pass a anonymous function as an argument to another function?

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

How can you get the type of arguments passed to a 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

How can you get the reference of a caller function inside 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

Which type of variable among global and local, takes precedence over other if names are same?

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

What is callback?

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.

Give an example of closure?

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

Which built-in method returns the character at the specified index?

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

Which built-in method calls a function for each element in the array?

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

Which built-in method returns the length of the string?

length() method returns the length of the string.

Which built-in method removes the last element from an array and returns that element?

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

Which built-in method reverses the order of the elements of an array?

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

Which built-in method returns the calling string value converted to lower case?

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

Which built-in method returns the calling string value converted to upper case?

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

Which built-in method returns the string representation of the number's value?

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

How typeof operator works?

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.

What typeof returns for a null value?

It returns "object".

Can you access Cookie using javascript?

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.