Hosting Solutions
mobile_logo Table of Contents

Syntax Guidelines

JavaScript ignores whitespace

The two lines below process similarly since JavaScript ignores whitespace. The first one uses less byte space, while the second one is easier for humans to read.

var person="Francis"; var person = "Francis" ;

JavaScript is case-sensitive

The two variables below are different due to the variation in uppercase and lowercase letters in the variable naming. They are treated as two separate string objects.

var myname = "Joe"; var MyName = "David";

Naming variables and functions

When creating variable and function names do not start them with a number. You can use numbers within the name, but just not as the first character. Another good rule of thumb is to use only letters, numbers and underscores when naming things.

// will cause script to fail var 3rd_name = "Erica"; // will process just fine var name_3 = "Erica";

Code comments

Comments are usually used for human reading only. They do not get processed by the browser software. They are handy for leaving yourself notes in large scripts, or for disabling whole sections of code. They can be placed anywhere in your JavaScript code.

<!-- This is a single line comment // This is a single line comment /* This is a multiline line comment (can span multiple lines) */

Semicolon ends statements

Even though JavaScript automatically terminates statements if we forget to, adding semicolon to terminate them is best practice.

var player_1 = "Cindy";

null - empty value

The literal value null is used to assign no initial value where JavaScript might expect one.

var obj = { a:500, b:null, c:200 }; alert( obj.b ); var x = null;

Dot notation and bracket notation

Either dot-notation or bracketed-string syntax provide access to properties and methods.

var str = "Hello"; alert( str.length ); alert( str["length"] ); window.alert("Hi"); window["alert"]("Hi");

( ) - grouping expressions

Grouping expressions together makes JavaScript interpret the grouping as a single expression or value. In math equations multiplication and division occur before addition and subtraction, grouping helps us structure mathematical equations properly.

var x = 5 + 3 * 2; /* 11 */ var x = (5 + 3) * 2; /* 16 */

\ - Escaping quote marks in strings

If we use double quotes to encapsulate our string we must escape any double quotes that need to reside in the string. The same logic applies to single quotes.

var str1 = "I watched \"The Avengers\" and it was cool."; var str2 = 'I watched \'The Avengers\' and it was cool.'; var str3 = "I watched 'The Avengers' and it was cool."; var str4 = 'I watched "The Avengers" and it was cool.'; document.write( str1 + "<br>" ); document.write( str2 + "<br>" ); document.write( str3 + "<br>" ); document.write( str4 + "<br>" );

\ - Escaping new lines in strings

If we attempt to add new lines to literal strings in JavaScript, we get "SyntaxError: unterminated string literal". This can be fixed by escaping the new lines in strings with a backslash.

var str = "How are you \ doing today?";

[ ] - array literal syntax

When arrays are made without the Array() constructor, square brackets are used to encapsulate the array elements.

var fruit = ["apple", "banana", "orange"];

{ } - object literal syntax

When objects are made without the Object() constructor, curly braces are used to encapsulate the properties.

var obj = { data1:"Hi", data2:50, data3:true };

JavaScript reserved words

There are some reserved words that are part of JavaScript syntax that we should try not use as variable/function/object/class names while writing code. In some cases using the reserved words can make your script operate unexpectedly.

List of reserved words:

await, break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, export, extends, finally, for, function, if, implements, import, in, instanceof, interface, new, package, private, protected, public, return, super, switch, this, throw, try, typeof, var, void, while, with, yield



IntroductionImplementationFallback ContentDevelopment OutputSyntax GuidelinesVariablesFunctionsStatementsOperatorsEvent HandlingObjects