JavaScript Syntax

JavaScript is case-sensitive

Everything in JavaScript including variables, function names, class names, and operators are case-sensitive. It means that counter and Counter variables are different.

Likewise, you cannot use  instanceof as the name of a function because it is a keyword. However, instanceOf is a valid function name.

Identifiers

An identifier is the name of a variable, function, parameter, or class. An identifier consists of one or more characters in the following format:

1. The first character must be a letter (a-z, or A-Z), an underscore(_), or a dollar sign ($).

2. The other characters can be letters (a-z, A-Z), numbers (0-9), underscores (_), and dollar            signs ($).

Note that the letter in this context is not limited to the ASCII character but may include extended ASCII or Unicode though it is not recommended.

It is a good practice to use camel case for the identifiers, meaning that the first letter is lowercase, and each additional word starts with a capital letter.

For example, the following names are identifiers:

counter inArray beginWith redirectPage

Comments

JavaScript supports both single-line and block comments.

A single-line comment starts with two forward-slash characters (//), for example:

// this is a single-line comment

A block comment starts with a forward slash and asterisk (/*) and ends with the opposite (*/) as follows:

/* * This is a block comment that can * span multiple lines */

It is a good practice to use an asterisk to begin the comment line for readability purposes.

Statements

Although JavaScript does not require to end a statement with a semicolon (;), it is recommended to always use the semicolon to end a statement.

The reason is that the semicolon will make your code more readable and helps you avoid many issues that you may encounter.

In addition, you may need to combine and compress the JavaScript code before deploying it to the production environment to remove extra white space to save the bandwidth; without the semicolons, you will have the syntax errors.

var a = 10; var b = 20;

You can use a code block that begins with a left curly brace ({) and ends with the right curly brace (}) to combine multiple statements as follows:

if( a > b) { console.log('a is greater than b'); return 1; }

Expressions

An expression is a piece of code that evaluates to a value. For example:

2 + 1

The above expression returns 3 so it is a valid expression.

Suppose you have two variables a and b, the following illustrates an expression that involves a and b:

a + b

Keywords & Reserved words

JavaScript defines a list of keywords and reserved words that have special uses. You cannot use the keywords and reserved words as the identifiers. The list of JavaScript keywords and reserved words is as follows:

abstractargumentsawaitboolean
breakbytecasecatch
charclassconstcontinue
debuggerdefaultdeletedo
doubleelseenumeval
exportextendsfalsefinal
finallyfloatforfunction
gotoifimplementsimport
ininstanceofintinterface
letlongnativenew
nullpackageprivateprotected
publicreturnshortstatic
superswitchsynchronizedthis
throwthrowstransienttrue
trytypeofvarvoid
volatilewhilewithyield

Now you should have a good understanding of the JavaScript syntax including naming an identifier, constructing statements, and making comments in code.



Post a Comment

0 Comments