You are on page 1of 8

1) What is JavaScript?

Ans:JavaScript is a scripting language most often used for client-side web development.

JavaScript

JavaScript is dynamic, high-level, untyped and interpreted language used in programming on the web. It
is popular and powerful programming language on the web. JavaScript is used by the majority of websites
and it is supported by modern browsers without having to make use of plug-ins.

It is prototype-based, with first class functions, which makes it a multi-paradigm language able to support
imperative, object-oriented and functional styles of programming. It is standardized in the ECMAScript
language specification.

2) What is the difference between JavaScript and Jscript?


Ans:Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft
reverse engineered Javascript and called it JScript.

3) Is JavaScript case sensitive?


Ans: Yes!
A function getElementById is not the same as getElementbyID.

4) What are the types used in JavaScript?


Ans: String, Number, Boolean, Function, Object, Null, Undefined.

5) What are the boolean operators supported by JavaScript?

And Operator: &&


Or Operator: ||
Not Operator: !

6) What is the difference between == and ===?


Ans:
== checks equality only,
=== checks for equality as well as the type.

7) How to create arrays in JavaScript?


Ans:There are two ways to create array in JavaScript like other languages:

a) The first way to create array


Declare Array:

var names = new Array();


Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";

b) This is the second way:


var names = new Array("Vikas", "Ashish", "Nikhil");

8) What does isNaN function do?


Ans: It returns true if the argument is not a number.
Example:

document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");

The output will be: true, true, false

9) What do you understand by this keyword in JavaScript?


Ans: In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context
that is placed on the stack. The following gives two different results (in the browser, where by-default the
window object is the 0-level context):

var obj = { outerWidth : 20 };

function say() {
alert(this.outerWidth);
}

say();//will alert window.outerWidth


say.apply(obj);//will alert obj.outerWidth

10) What does "1"+2+4 evaluate to?


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

11) What does 3+4+"7" evaluate to?


Ans: Since 3 and 4 are integers, this is number arithmetic, since 7 is a string, it is concatenation, so 77 is
the result.

12) Does JavaScript support foreach loop?


Ans: JavaScript 1.6(ECMAScript 5th Edition) support foreach loop,

13) How you will add function as a property in a JavaScript object? Give an example.
Ans:

var man = new Object();


man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
man.getName = function() { return man.name;}
console.log(man.getName()
); // Logs 'Vikas Ahlawat'.
14) What is this?

var myArray = [[[]]];


Ans: Three dimensional array

15)Does JavaScript Support automatic type conversion, If yes give example.

Ans: Yes! Javascript support automatic type conversion. You should take advantage of it, It is most
common way of type conversion used by Javascript developers.

var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string" typeof(a); //"number" typeof(b); //"number"

16A) What is an object in Javascript


An Object is a collection of properties, and a property is an association between a name and a value,
A Property value can be a function, In which case the property is known as a method.

16) How many ways we can create javascript object


Ans: You can create define and create your own objects.
There are different ways to create new objects.
A single object, using an object literal
A single object, with the keyword new
An object constructor, and the create objects of the constructed

JavaScript objects don't have methods. They just have properties, and any property could be a
function.
a) Using an object Literal:
Using an object literal, you both define and create an object in one statement.
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

b) Using the JS keyword NEW:


var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";

The two example above do exactly the same. There is no need to use new object().
For simplicity, readability and execution speed, use the first one.(the object literal method)
c) Using an Object Constructor:
The standard way to create an object type is to use an object constructor function.
function person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50,"blue");
var myMother = new person("Sally", "Rally", 48,"green");
17) What is the prototype in JavaScript
All JavaScript objects inherit the properties and methods from their prototype. Objects created using
an object literal, or with new Object(), Inherit from a prototype called Object. prototype.

18) What are JavaScript types?

Here are the JavaScript types

Number, Object, String, Boolean, Function, Null, Undefined

19) What is the difference between ViewState and SessionState?

ViewState is specific to a page in session.

SessionState relates to user specific data which one can access across all pages in the web application.

20) What do you mean by NULL in JavaScript?

NULL represents no value or no object. It simply means no object or null string, no array object, no
number and no valid Boolean value.

21) What are all the types of Pop up boxes available in JavaScript?

They include

Alert
Confirm, and
Prompt

22) Difference between dom and bom

Ans: The Browser Object Model is a larger representation of everything provided by the browser
including the current document, location, history, frames, and any other functionality the browser may
expose to JavaScript. The Browser Object Model is not standardized and can change based on
different browsers.

The Document Object Model is standardized and is specific to current HTML document. It is exposed
by the Browser Object Model (i.e., DOM is a subset of BOM).

23) How to read elements of an array in JavaScript?

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]
}
24) What is a named function in JavaScript? How to define a named function?

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
}
25) How many types of functions JavaScript supports?

A function in JavaScript can be either named or anonymous.

Functions are first-class objects, because they can have properties and methods just like any
other object

A function is an instance of the Object type


You can store the function in a variable
You can pass the function as a parameter to another function
You can return the function from a function
26) 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.

27) Can you assign an anonymous function to a variable?

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

28) What is the purpose of 'this' operator in JavaScript?

JavaScript famous keyword this always refers to the current context.


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

JavaScript have function scope not block scope

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.

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

32) 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

.33) What is closure?

A closure is an inner function that has access to the outer (enclosing) function's variablesscope
chain

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

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

36) 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

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

37) How to redirect a url using JavaScript?

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>
38) How to print a web page 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.

39) How to handle exceptions in JavaScript?

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.

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

41) What is namespace in JavaScript?

Namespacing is used to avoid polluting the global namespace (no window. variables). In truth, each
namespace is just a big variable that has many properties and methods.

Prevents global scope pollution


Organizes the code
Separates application parts
Helps in writing modular code

42) What is a Closure in JavaScript?

A closure is an inner function that has access to the outer (enclosing) function's variablesscope
chain. The closure has three scope chains: it has access to its own scope (variables defined between
its curly brackets), it has access to the outer function's variables, and it has access to the global
variables

43)

You might also like