Web Development Interview Questions – Set 02

How to hide javascript code from old browsers that dont run it?

Use the below specified style of comments

<script language=javascript>

<!–javascript code goes here–>

Or

Use the <NOSCRIPT>some html code </NOSCRIPT> tags and code the display html statements between these and this will appear on the page if the browser does not support javascript.

How to read and write a file using javascript?

I/O operations like reading or writing a file is not possible with client-side javascript. However , this can be done by coding a Java applet that reads files for the script.

Does javascript have the concept level scope?

No.Javascript does not have block level scope,all the variables declared inside a function possess the same level of scope unlike c,c++,java.

Is a javascript script faster than an ASP script?

Yes.Since javascript is a client-side script it does require the web server’s help for its computation,so it is always faster than any server-side script like ASP,PHP,etc.

Can javascript code be broken in different lines?

Breaking is possible within a string statement by using a backslash at the end but notwithin any other javascript statement.that is,

document.write(“Hello world”);

is possible but not

document.write(“hello world”);

Types of CSS

Most commonly used types of CSS are:

1. Local Style (inline style)
It is defined with-in a tag using an attribute style. It’s scope is limited to a tag in
which it is defined.

2. Internal Style
It is defined with-in a head block using a tag style. Its scope is limited to all those
tags, which are with-in that page.

3. External Style
It is defined in a separate file and saved with extension (.CSS). Its scope is
limited to all those web pages, which are linked to that CSS file.

What are Javascript closures?When would you use them?

Two one sentence summaries:

  • a closure is the local variables for a function – kept alive after the function has returned

or

  • a closure is a stack-frame which is not deallocated when the function returns.

A closure takes place when a function creates an environment that binds local variables to it in such a way that they are kept alive after the function has returned. A closure is a special kind of object that combines two things: a function, and any local variables that were in-scope at the time that the closure was created.
The following code returns a reference to a function:

function sayHello2(name)

{

var text = ‘Hello ‘ + name; // local variable

var sayAlert = function() { alert(text); }

return sayAlert;

}

Closures reduce the need to pass state around the application. The inner function has access to the variables in the outer function so there is no need to store the information somewhere that the inner function can get it.

This is important when the inner function will be called after the outer function has exited. The most common example of this is when the inner function is being used to handle an event. In this case you get no control over the arguments that are passed to the function so using a closure to keep track of state can be very convenient.

How to comment javascript code?

Use

// for line comments and

/* some code here */ for block comments

Name the numeric constants representing max,min values

Number.MAX_VALUE
Number.MIN_VALUE

How to detect the operating system on the client machine?

In order to detect the operating system on the client machine, the navigator.appVersionstring (property) should be used.

What are undefined and undeclared variables?

Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .

Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.