JavaScript HelpDesk

Javascript Interview Questions


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

Java is a complete programming language. In contrast, JavaScript is a coded program that can be introduced to HTML pages. These two languages are not at all inter-dependent and are designed for the different intent. Java is an object – oriented programming (OOPS) or structured programming language like C++ or C whereas JavaScript is a client-side scripting language and it is said to be unstructured programming.

Following are the JavaScript types:

  • Number
  • String
  • Boolean
  • Function
  • Object
  • Null
  • Undefined

isNan function returns true if the argument is not a number otherwise it is false.

JavaScript is faster. JavaScript is a client-side language and thus it does not need the assistance of the web server to execute. On the other hand, ASP is a server-side language and hence is always slower than JavaScript. Javascript now is also a server side language (nodejs).

Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.

Breaking within a string statement can be done by the use of a backslash, ‘\’, at the end of the first line

Example:

document.write("This is \a program");

Netscape is the software company who developed 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.

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.

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.

Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.

Example:

// Declare a global globalVariable = “Test”;

The problems that are faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.

A prompt box is a box which allows the user to enter input by providing a text box. Label and box will be provided to enter the text or number.

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

var emp = {
   name: "Sunil Guleria",
   age: 38
};

‘this’ keyword refers to the object from where it was called.

Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.

The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs the timer to stop.

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
}
// for Single line comments and

/* Multi Line Comment */

=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.

To submit a form using JavaScript use document.form[0].submit();

document.form[0].submit();

A function in JavaScript can be either named or anonymous.

JavaScript variable arguments represents the arguments passed to a function.

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

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.

Yes JavaScript does support automatic type conversion, it is the common way of type conversion used by JavaScript developers

Following are looping structures in Javascript:

  • For
  • While
  • do-while loops

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:

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

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

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.

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.

The functionality of delete operator is used to delete all variables and objects in a program but it cannot delete variables declared with VAR keyword.

All variables in the JavaScript are object data types.

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.

alert(document.getElementById(‘checkbox1’).checked);

If the CheckBox will be checked, this alert will return TRUE.

The for-in loop is used to loop through the properties of an object.

for (variable name in object){
 
statement or block to execute
}

The onload function is not run until all the information on the page is loaded. This leads to a substantial delay before any code is executed.

onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.

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 string split() function to break the string into key and values.