Search within TutorialsPark

Javascript Syntax


Basic Syntax and Standards for writing Javascript codes.


Case Sensitivity

1. Unlike HTML tags, everything in Javascript is case sensitive.All Javascript names, variables, keywords, objects, functions etc are case sensitive.Incorrect syntax will throw an error or it will be ignored simply.

2. For example, the boolean word false is not the same as FaLSe. Most names favour a lowercase form, though some use a combination of upper and lowercase (eg: Array.slice, onMouseOver, getElementById etc).


Javascript Identifiers

1. Javascript identifiers can be the name of a variable, function , property, or function argument. It can comprise of one or more characters satisfying the following format

* All identifiers must begin with a letter, dollar sign or an underscore()

* The rest of the characters can be letters, dollar sign, underscore or numbers.

2. By convention though not strictly enforced, ECMAScript identifiers use camel-case, meaning that first letter begins with lowercase and every additional letter is offset by Capital letters. Eg: letsGo, lifeIsBeautiful


Javascript Comments

1. The purpose of a comment is to describe or explain the program.Javascript uses C style comments for both single-line and block comments.

2. A single comment starts with two forward slashes.Any text between a (//) and the end of the line is treated as a comment and hence ignored by Javascript.

3. A block comment starts with a forward slash and asterisk (*/) and closing tag (/*)

Example: Javascript - Comment

Note: Javascript comments can be used to Disable Scripts from being executed


Javascript Statements and Semicolons

1. All languages are made up of statements, Javascript is made up of statements comprise of expression which are executed from top to bottom.

2. Multiple statements must be seperated using a semicolon (;), though the parsers can determine where the statement ends but its recommended to always use one.

3. Also multiple statements can be combined into a single code block by using the curly braces, starting with opening braces ({) and ending with closing braces (})

Example: Javascript - Statements and Semicolons

Note: Code blocks are used mainly with control statements like if/else , for etc


Javascript whitespaces

1. In JavaScript, whitespace in between the words are ignored.But name of functions such as onClick() , toUpperCase() etc cannot contain whitespace even though it comprises of more than one word.

2. Whitespace is preserved within double quotes. All keywords, reserved words, statements etc cannot have any whitespace in between words as the word gets distorted.

Example: Javascript - Whitespaces

Note: Since extra whitespaces get ignored, users can create line breaks, indents or organise programs etc.


Javascript keywords and Reserved words

1. JavaScript reserves a number of identifiers as keywords in the language.These keywords cannot be used in the statements as identifiers or property names.

2. The complete list of reserved words defined in ECMA-262 is as follows.

Example: CSS Display - none

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.


Asynchronous Javascript

1. The <script> element's attribute async is similar to defer, that is it delays the execution of the scripts till closing </html> tag is parsed while beginning to download the files as soon as the page is loaded.

2. The only difference with attribute defer is that, the order of execution of scripts is not as specified.

Example: Javascript Attribute - async

Give it a TRY! » Note: The attribute defer is supported only for external Javascript files and not inline Javascript.