GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikibooks.org — JavaScript/Lexical structure - Wikibooks, open books for an open world
Summary
Case SensitivityJavaScript is case-sensitive. This means that all keywords, variable names, and function names must be written consistently. If you create a function WhitespacesWhitespace 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. function filterEmailKeys(event){ event=event||window.event; const charCode=event.charCode||event.keyCode; const char=String.fromCharCode(charCode); if(/[a-zA-Z0-9_\-\.@]/.exec(char)){return true;} return false; } The following is the same script with a typical amount of whitespace. The following is the same script with a typical amount of whitespace. function filterEmailKeys(event) { event = event || window.event; const charCode = event.charCode || event.keyCode; const char = String.fromCharCode(charCode); if (/[a-zA-Z0-9_\-\.@]/.exec(char)) { return true; } return false; } The following is the same script with a lot of whitespaces. function filterEmailKeys( evt ) { evt = evt || window.event; const charCode = evt.charCode || evt.keyCode; const char = String.fromCharCode ( charCode ); if ( /[a-zA-Z0-9_\-\.@]/.exec ( char ) ) { return true; } return false; } CommentsComments 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 // Show a welcome message alert("Hello, World!")
Multi-line comments start with Here is an example of how to use the different types of commenting techniques. /* This is a multi-line comment that contains multiple lines of commented text. */ let a = 1; /* commented out to perform further testing a = a + 2; a = a / (a - 3); // is something wrong here? */ alert('a: ' + a); /* This comment has two /* but they're both canceled out by */ SemicolonsIn 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 called automatic semicolon insertion. // the line x = 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. LiteralsA 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: const myLiteral = "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: const myObject = { name:"value", anotherName:"anotherValue" }; const isClosed = true; const mayBeWrong = "true"; Details of these different types are covered in Variables and Types. IdentifiersAn identifier is a name for a piece of data, such as a variable, array, or function. There are rules:
Examples of valid identifiers:
An example of 'identifiers' are variable names. They obey such rules.
ExercisesNotesReferences
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |