javascript interview questions

Top javascript frequently asked interview questions

How can I upload files asynchronously?

I would like to upload a file asynchronously with jQuery. This is my HTML:

<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>

And here my Jquery code:

$(document).ready(function () {
    $("#uploadbutton").click(function () {
        var filename = $("#file").val();

            type: "POST",
            url: "",
            enctype: 'multipart/form-data',
            data: {
                file: filename
            success: function () {
                alert("Data Uploaded: ");

Instead of the file being uploaded, I am only getting the filename. What can I do to fix this problem?

Current Solution

I am using the jQuery Form Plugin to upload files.

Source: (StackOverflow)

How do I decide when to use Node.js? [closed]

I am new to this kind of stuff, but lately I've been hearing a lot about how good Node.js is. Considering how much I love working with jQuery and JavaScript in general, I can't help but wonder how to decide when to use Node.js. The web application I have in mind is something like Bitly - takes some content, archives it.

From all the homework I have been doing in the last few days, I obtained the following information. Node.js

  • is a command-line tool that can be run as a regular web server and lets one run JavaScript programs
  • utilizes the great V8 JavaScript engine
  • is very good when you need to do several things at the same time
  • is event-based so all the wonderful Ajax-like stuff can be done on the server side
  • lets us share code between the browser and the backend
  • lets us talk with MySQL

Some of the sources that I have come across are:

Considering that Node.js can be run almost out-of-the-box on Amazon's EC2 instances, I am trying to understand what type of problems require Node.js as opposed to any of the mighty kings out there like PHP, Python and Ruby. I understand that it really depends on the expertise one has on a language, but my question falls more into the general category of: When to use a particular framework and what type of problems is it particularly suited for?

Source: (StackOverflow)

What is the difference between call and apply?

What is the difference between using call and apply to invoke a function?

var func = function(){



Are there performance differences between the two methods? When is it best to use call over apply and vice versa?

Source: (StackOverflow)

Validate email address in JavaScript?

How can an email address be validated in JavaScript?

Source: (StackOverflow)

Capitalize the first letter of string in JavaScript

How do I capitalize the first character of a string, but not change the case of any of the other letters?

For example:

  • this is a test -> This is a test
  • the Eiffel Tower -> The Eiffel Tower
  • /index.html -> /index.html

Source: (StackOverflow)

How do I return the response from an asynchronous call?

I have a function foo which makes an Ajax request. How can I return the response from foo?

I tried to return the value from the success callback as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.

function foo() {
    var result;

        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well

    return result;

var result = foo(); // It always ends up being `undefined`.

Source: (StackOverflow)

For-each over an array in JavaScript?

How can I loop through all the objects in an array using JavaScript?

I thought of something like this (where objects is my array of objects):

forEach(instance in objects)

But this does not seem to be correct.

Source: (StackOverflow)

How do you get a timestamp in JavaScript?

How can I get a timestamp in JavaScript?

Something similar to Unix's timestamp, that is, a single number that represents the current time and date. Either as a number or a string.

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)

Detecting an undefined object property

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

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:


How can I get the child img using a selector?

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)

array.contains(obj) in JavaScript

What is the most concise and efficient way to find out if a JavaScript array contains an obj?

This is the only way I know to do it:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
    return false;

Is there a better and more concise way to accomplish this?

This is very closely related to Stack Overflow question Best way to find an item in a JavaScript Array? which addresses finding objects in an array using indexOf.

Source: (StackOverflow)

event.preventDefault() vs. return false

When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well:

1. event.preventDefault()

$('a').click(function (e) {
    // custom handling here

2. return false

$('a').click(function () {
    // custom handling here
    return false;

Is there any significant difference between those two methods of stopping event propagation?

For me, return false; is simpler, shorter and probably less error prone than executing a method. With the method, you have to remember about correct casing, parenthesis, etc.

Also, I have to define the first parameter in callback to be able to call the method. Perhaps, there are some reasons why I should avoid doing it like this and use preventDefault instead? What's the better way?

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)