const x = 0; const X = 0; defines two different variables;CONST x = 0; leads to a syntax error.//. Multi-line comments are surrounded with/* */.const 1x = 0; leads to a syntax error.JavaScript is case-sensitive. This means that all keywords, variable names, and function names must be written consistently. If you create a functionHello() it is different from the functionHELLO(),hello(), orhEllo(). Or, the statementIF (x > 0) { } leads to a syntax error because the keywordif is defined in lower-case.
Whitespace includes spaces, tabs, and line breaks.[note 1] If there is a sequence of multiple whitespaces, JavaScript reduces them to a single whitespace, e.g: ' ' -> ' ', '\n\t' -> '\n'. This single remaining whitespace delimits the language elements like keywords and variable names, ... from each other. The resulting source code is hard to read for people[2] but (a little) easier to parse by the browser. The main advantage is the smaller size of the code which must be transferred between server and client.
The following script is an example with very little whitespace.
functionfilterEmailKeys(event){event=event||window.event;constcharCode=event.charCode||event.keyCode;constchar=String.fromCharCode(charCode);if(/[a-zA-Z0-9_\-\.@]/.exec(char)){returntrue;}returnfalse;}
The following is the same script with a typical amount of whitespace.
The following is the same script with a typical amount of whitespace.
functionfilterEmailKeys(event){event=event||window.event;constcharCode=event.charCode||event.keyCode;constchar=String.fromCharCode(charCode);if(/[a-zA-Z0-9_\-\.@]/.exec(char)){returntrue;}returnfalse;}
The following is the same script with a lot of whitespaces.
functionfilterEmailKeys(evt){evt=evt||window.event;constcharCode=evt.charCode||evt.keyCode;constchar=String.fromCharCode(charCode);if(/[a-zA-Z0-9_\-\.@]/.exec(char)){returntrue;}returnfalse;}
Comments are parts of the source code that will - per definition - not be executed.
They allow you to leave notes in your code to help other people understand it. They also allow you to comment out code that you want to hide from the parser but you don't want to delete.
A double slash// turns all of the following text on the same line into a comment that will not be processed by the JavaScript interpreter.
// Show a welcome messagealert("Hello, World!")
Multi-line comments start with/* and end with the reverse*/. Multi-line comments don't nest.
Here is an example of how to use the different types of commenting techniques.
/* This is a multi-line commentthat contains multiple linesof commented text. */leta=1;/* commented out to perform further testinga = a + 2;a = a / (a - 3); // is something wrong here?*/alert('a: '+a);/* This comment has two /* but they're both canceled out by */
In many programming languages, semicolons are required at the end of each code statement. In JavaScript, the use of semicolons is optional, as a new line indicates the end of the statement (with some exceptions). This is calledautomatic semicolon insertion.
// the linex=a+b;// is equivalent to:x=a+b
But the exceptions can be quite surprising.[3] Automatic semicolon insertion can create hard to debug problems.
a=b+c(d+e).print()
The above code is not interpreted as two statements. Because of the parentheses on the second line, JavaScript interprets the above as if it were
a=b+c(d+e).print();
when instead, you may have meant it to be interpreted as
a=b+c;(d+e).print();
Even though semicolons are optional, it's preferable to end statements with a semicolon to prevent any misunderstandings from taking place.
A literal is a hard-coded value. Literals provide a means of expressing specific values in your script. For example, to the right of the equals sign:
constmyLiteral="a fixed value";
There are several types of literals available. The most common are the string literals, but there are also numeric literals, booleans, undefined, null, regex literals, array literals, and object literals.
Examples of an object, a boolean, and a string literal:
constmyObject={name:"value",anotherName:"anotherValue"};constisClosed=true;constmayBeWrong="true";
Details of these different types are covered inVariables and Types.
An identifier is a name for a piece of data, such as a variable, array, or function. There are rules:
Examples of valid identifiers:
u$hello_Hellohello901A2B3C is an invalid identifier, as it starts with a number.
An example of 'identifiers' are variable names. They obey such rules.