smallseo.info

javascript interview questions

Top javascript frequently asked interview questions

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent.

The previous developer uses two ways of declaring functions and I can't work out if there is a reason behind it or not.

The two ways are:

var functionOne = function() {
    // Some code
};
function functionTwo() {
    // Some code
}

What are the reasons for using these two different methods and what are the pros and cons of each? Is there anything that can be done with one method that can't be done with the other?


Source: (StackOverflow)

Why does Google prepend while(1); to their JSON responses?

Why does Google prepend while(1); to their (private) JSON responses?

For example, here's a response while turning a calendar on and off in Google Calendar:

while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
  ['remindOnRespondedEventsOnly','true'],
  ['hideInvitations_remindOnRespondedEventsOnly','false_true'],
  ['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]

I would assume this is to prevent people from doing an eval() on it, but all you'd really have to do is replace the while and then you'd be set. I would assume the eval prevention is to make sure people write safe JSON parsing code.

I've seen this used in a couple of other places, too, but a lot more so with Google (Mail, Calendar, Contacts, etc.) Strangely enough, Google Docs starts with &&&START&&& instead, and Google Contacts seems to start with while(1); &&&START&&&.

What's going on here?


Source: (StackOverflow)

Validate decimal numbers in JavaScript - IsNumeric()

What's the cleanest, most effective way to validate decimal numbers in JavaScript?

Bonus points for:

  1. Clarity. Solution should be clean and simple.
  2. Cross-platform.

Test cases:

 01. IsNumeric('-1') => true
 02. IsNumeric('-1.5') => true
 03. IsNumeric('0') => true
 04. IsNumeric('0.42') => true
 05. IsNumeric('.42') => true
 06. IsNumeric('99,999') => false
 07. IsNumeric('0x89f') => false
 08. IsNumeric('#abcdef')=> false
 09. IsNumeric('1.2.3') => false
 10. IsNumeric('') => false
 11. IsNumeric('blah') => false

Source: (StackOverflow)

How can I get query string values in JavaScript?

Is there a plugin-less way of retrieving query string values via jQuery (or without)?

If so, how? If not, is there a plugin which can do so?


Source: (StackOverflow)

Href attribute for JavaScript links: "#" or "javascript:void(0)"?

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.?

function myJsFunc() {
    alert("myJsFunc");
}
<a rel='nofollow' href="#" onclick="myJsFunc();">Run JavaScript Code</a>

or

function myJsFunc() {
    alert("myJsFunc");
}
 <a rel='nofollow' href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>


Source: (StackOverflow)

Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement.

Is there a performance benefit to replacing == with ===?

Any performance improvement would be welcomed as many comparison operators exist.

If no type conversion takes place, would there be a performance gain over ==?


Source: (StackOverflow)

How can I check if one string contains another substring?

How can I check if one string contains another substring in JavaScript? Usually, I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?


Source: (StackOverflow)

Checking if an element is hidden

In jQuery, it is possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle().

Using jQuery, how would you test if an element is visible or hidden?


Source: (StackOverflow)

What does "use strict" do in JavaScript, and what is the reasoning behind it?

Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error:

Problem at line 1 character 1: Missing "use strict" statement.

Doing some searching, I realized that some people add "use strict"; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not reveal much of the history behind this string statement. Certainly it must have something to do with how the JavaScript is interpreted by the browser, but I have no idea what the effect would be.

So what is "use strict"; all about, what does it imply, and is it still relevant?

Do any of the current browsers respond to the "use strict"; string or is it for future use?


Source: (StackOverflow)

How can I make a page redirect using jQuery?

How can I redirect the user from one page to another using jQuery?


Source: (StackOverflow)

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts which make up them (for example, functions, variables and the like), but does not understand closures themselves?

I have seen the Scheme example given on Stack Overflow, and it did not help.


Source: (StackOverflow)

What is the most efficient way to clone an object?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's currently Firefox-only. In Mootools 1.2, I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency.

I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists.


Source: (StackOverflow)

$(this) selector and children?

I have a layout similar to this:

<div id="..."><img src="..."></div>

and would like to use a jQuery selector to select the child img inside the div on click.

To get the div, I've got this selector:

$(this)

How can I get the child img using a selector?


Source: (StackOverflow)

Detecting an undefined object property

What's the best way of checking if an object property in JavaScript is undefined?


Source: (StackOverflow)

Create GUID / UUID in JavaScript?

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc..

The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around.


Source: (StackOverflow)