Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is purpose of onError event handler in JavaScript?

Answer»

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.

2.

How to handle exceptions in JavaScript?

Answer»

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.

3.

What is Number object in JavaScript?

Answer»

he Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.

Syntax −

Creating a number object −

var val = new Number(number);

If the argument cannot be converted into a number, it returns NaN (Not-a-Number).

4.

What is Date object in JavaScript?

Answer»

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.

5.

How to print a web page using javascript?

Answer»

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.

6.

How to redirect a url using JavaScript?

Answer»

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>
7.

How to delete a Cookie using JavaScript?

Answer»

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.

8.

How to read a Cookie using JavaScript?

Answer»

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.

9.

How to create a Cookie using JavaScript?

Answer»

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

Syntax −

document.cookie = "key1 = value1; key2 = value2; expires = date";

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.

10.

Can you access Cookie using javascript?

Answer»

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.

11.

What typeof returns for a null value?

Answer»

It returns "object".

12.

How typeof operator works?

Answer»

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.

13.

What are the variable naming conventions in JavaScript?

Answer»

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.

14.

Is JavaScript a case-sensitive language?

Answer»

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.

15.

What are disadvantages of using JavaScript?

Answer»

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.

16.

What are the advantages of using JavaScript?

Answer»

Following are the advantages of using 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.

17.

Name some of the JavaScript features.

Answer»

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.