309
votes
24answers
73k views

When to Use Double or Single Quotes in JavaScript

console.log("double"); vs console.log('single'); I see more and more JavaScript libraries out there using single quotes when handling strings. What are the reasons to use one over the other? I ...
300
votes
27answers
17k views

Is there a better way of writing v = (v == 0 ? 1 : 0);

I want to toggle a variable between 0 and 1. If it's 0 I want to set it to 1, else if it's 1 I want to set it to 0. This is such a fundamental operation that I write so often I'd like to investigate ...
106
votes
8answers
22k views

Are there any coding standards for JavaScript? [closed]

What are the established coding standards for JavaScript?
101
votes
7answers
62k views

typeof === “undefined” vs. != null

I often see JavaScript code which checks for undefined parameters etc. this way: if (typeof input !== "undefined") { // do stuff } This seems kind of wasteful, since it involves both a type ...
84
votes
21answers
24k views

When is JavaScript's eval() not evil?

I'm writing some JavaScript to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result. ...
53
votes
5answers
4k views

What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported. (function() { ... code ... })() ...
49
votes
8answers
34k views

Declaring Multiple Variables in JavaScript

In JavaScript, it is possible to declare multiple variables like this: var variable1 = "Hello World!"; var variable2 = "Testing..."; var variable3 = 42; ...or like this: var variable1 = "Hello ...
42
votes
5answers
6k views

Coding Style Guide for node.js apps?

Is there a (or several) coding style guide for node.js? If not, what are the emerging styles used by the top open-source node projects? I'm looking for a guide (or several guides) along the lines of ...
39
votes
7answers
12k views

Unobtrusive JavaScript: <script> at the top or the bottom of the HTML code?

I've recently read the Yahoo manifesto Best Practices for Speeding Up Your Web Site. They recommend to put the JavaScript inclusion at the bottom of the HTML code when we can. But where exactly and ...
32
votes
12answers
1k views

Is it better to wrap code into an 'IF' statement, or is it better to 'short circuit' the function and return? [closed]

I'm doing some coding in JavaScript, and I am having a lot of instances where I have to check some stuff before I proceed. I got into the habit of returning early in the function, but I'm not sure ...
27
votes
6answers
3k views

Why use semicolon?

Are there any reasons, apart from subjective visual perception and cases where you have multiple statements on the same line, to use semicolon at the end of statements in Javascript? It looks like ...
20
votes
5answers
25k views

style.display='none' doesnt work on option tags in chrome, but it does in firefox, anyone know why? or a workaround?

ok, heres some sample code that demonstrates the problem. if i click the button in firefox, the first option disappears. if i click the button in chrome, nothing happens, or rather if i inspect the ...
17
votes
8answers
347 views

I want to know if the two JavaScript snippets are the same or not

I am new to JavaScript and I want learn more about it. For example: a == true && alert("a is true"); Is this similar to: if(a == true) { alert("a is true"); } If the above code is ...
15
votes
11answers
15k views

Javascript: Inline function vs predefined functions

Can any body throw me some arguments for using inline functions against passing predefined function name to some handler. I.e. which is better: (function(){ setTimeout(function(){ /*some code ...
15
votes
3answers
478 views

Best way to require several modules in NodeJS

I don't much like the standard way to require modules, which goes something like this: connect = require 'connect' express = require 'express' redis = require 'redis' sys = require 'sys' coffee = ...

1 2 3 4 5 21
15 30 50 per page