JavaScript Interview Questions

Level: Freshers

Q 1 - What is JavaScript?
 

JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object Oriented Programming language

Q 2 - Who developed JavaScript?
 

Netscape is the software company who developed JavaScript.

Q 3 - What are the JavaScript features.

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 omplementary to and integrated with HTML.
  • JavaScript is open and cross-platform.

Q 4 - What are the advantages of using JavaScript?

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.

Q 5 - What are disadvantages of using JavaScript?
 

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.

Q 6 - What are the valid scopes of a variable in JavaScript?

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

Q 7 - What are the variable naming conventions in JavaScript?
 

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

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

Q 9 - How to create a Cookie using JavaScript?

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

Q 10 - How to redirect a url 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 –  
Example: &lt;head&gt;<br /> &lt;script type=&quot;text/javascript&quot;&gt;<br /> &lt;!--<br /> &nbsp; &nbsp;window.location=&quot;http://www.newlocation.com&quot;;<br /> //--&gt;<br /> &lt;/script&gt;<br /> &lt;/head&gt;

Q 11 - What is purpose of onError event handler in JavaScript?

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.

Q 12 - What are JavaScript types?

Following are the JavaScript types:
  • Number
  • String
  • Boolean
  • Function
  • Object
  • Null
  • Undefined

Q 13 - What are undeclared and undefined variables?

  • Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.
  •   Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned
     

Q 14 - What is ‘this’ keyword in JavaScript?

'This’ keyword refers to the object from where it was called.

Q 15 - What are all the looping structures in JavaScript?

Following are looping structures in Javascript:
  •     For
  •     While
  •     do-while loops
     

Q 16 - What would be the result of 3+2+”7″?

Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will be done. So the result would be 57
 

Q 17 - What do mean by NULL in Javascript?

The NULL value is used to represent no value or no object.  It implies no object or null string, no valid boolean value, no number and no array object.
 

Q 18 - What is the difference between an alert box and a confirmation box?
 

  • An alert box displays only one button which is the OK button.
  •  But a Confirmation box displays two buttons namely OK and cancel.

Q 19 - What is break and continue statements?
 

  •     Break statement exits from the current loop.
  •     Continue statement continues with next statement of the loop.
     

Q 20 - What are the two basic groups of dataypes in JavaScript?
 

They are as –
  •     Primitive
  •     Reference types.
Primitive types are number and Boolean data types. Reference types are more complex types like strings and dates.