Professional Documents
Culture Documents
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.
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");
function say() {
alert(this.outerWidth);
}
13) How you will add function as a property in a JavaScript object? Give an example.
Ans:
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"
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"};
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.
SessionState relates to user specific data which one can access across all pages in the web application.
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
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).
An array has a length property that is useful for iteration. We can read elements of an array as follows
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?
Functions are first-class objects, because they can have properties and methods just like any
other object
An anonymous function can be defined in similar way as a normal function but it would not have any
name.
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.
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.
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
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?
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.
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
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.
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.
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.
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)