JavaScript syntax is case-sensitive. For instance, a function may not be named "instanceof()" because that, in JavaScript, is a keyword, but it may be, without a problem, named "instanceOf()".
The syntax is made of three basic components:
- identifiers;
- comments;
- statements.
Identifiers
An identifier refers to the name of a variable, function, property or function argument.
Following rules apply to identifiers:
- The first character must be a letter, an underscore ("_") or a dollar sign ("$");
- All other characters may be letters, underscores, dollar signs, or numbers.
The best practice for naming an identifier and follow ECMA suggestions is to make the first word in lowercase, while all following ones are capitalized; for instance function ourCoolFunction().
Note that the reserved words and keywords true, false and null cannot be used as identifiers.
Comments
A typical C-style comments are applied to the JavaScript syntax comments, both single-line and block comments.
Single-line comment:
//this is a single line comment
Block comment:
/*
this multi-line part of
the script is
a comment
*/
Statements
Statements are the crucial part of JavaScript syntax; they are actually the code itself.
Although not required, it is suggested to terminate each statement with a semicolon (";"). That will prevent mistakes during code compressing and parsing, as well as make it more readable.
These are some examples of JS statements:
var sentence = "Hello" + "World"; //statement #1
var sum = a + b; // statement #2
if (sum > 10) { // statement #3
alert ("The sum is above 10!");
sum = 0;
}
Example
Basic JavaScript syntax:
Comments
No comments have been made yet.
Please login to leave a comment. Login now