Javascript Language
JavaScript, often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have a dedicated JavaScript engine that executes the client code. These engines are also utilized in some servers and a variety of apps. The most popular runtime system for non-browser usage is Node.js.
JavaScript is a high-level, often just-in-time compiled language that conforms to the ECMAScript standard. It has dynamic typing, prototype-based object-orientation, and first-class functions. It is multi-paradigm, supporting event-driven, functional, and imperative programming styles. It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).
The ECMAScript standard does not include any input/output (I/O), such as networking, storage, or graphics facilities. In practice, the web browser or other runtime system provides JavaScript APIs for I/O.
Although Java and JavaScript are similar in name and syntax, the two languages are distinct and differ greatly in design.
Variables
In Javascript there are basically three ways to declare a variable:
var
Declares variables with function or global scope and allows re-declaration and updates within the same scope.
let
Declares variables with block scope, allowing updates but not re-declaration within the same block.
const
Declares block-scoped variables that cannot be reassigned after their initial assignment.
Block Scope
Scope, with regards to variables, has to do with where the variables are visible or useable to the code. Global scope means that it is visible and useable anywhere within the code. Block scope is contained to the block where the declaration exists. Blocks are enclosed between braces {...}
Control Flow
All computer programming languages have a set of different statements that define the flow of a program. In many of them, the syntax is the same.
If Then Else
Switch
Do While
While Do
For Next
For In
For Of
Data Structures & Types
The latest ECMAScript standard defines eight data types. Seven are known as Primatives and the eighth is the javascript Object.
Primatives | |
---|---|
Boolean | True or false. |
null | A special keyword denoting a null value. (Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.) |
undefined | A top-level property whose value is not defined. |
Number | An integer or floating point number. For example: 42 or 3.14159. |
BigInt | An integer with arbitrary precision. For example: 9007199254740992n. |
String | A sequence of characters that represent a text value. For example: "Howdy". |
Symbol | A data type whose instances are unique and immutable. |
Non-Primative | |
Object | In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String value or a Symbol value. There are two types of object properties: The data property and the accessor property. |
Literals
Literals represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script.
Boolean Literals
Object Literals
String Literals
Literal Examples
Object Literals
An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).
Note: Do not use an object literal at the beginning of a statement! This will lead to an error (or not behave as you expect), because the curly brace will be interpreted as the beginning of a block.
The following is an example of an object literal. The first element of the car object defines a property, myCar, and assigns to it a new string, "Saturn"; the second element, the getCar property, is immediately assigned the result of invoking the function (carTypes("Honda")); the third element, the special property, uses an existing variable (sales).
Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.
Template Literals
Template literals are enclosed by the back-tick (`) (grave accent) character instead of double or single quotes. Template literals provide syntactic sugar for constructing strings. (This is similar to string interpolation features in Perl, Python, and more.)
Tagged templates are a compact syntax for specifying a template literal along with a call to a "tag" function for parsing it. A tagged template is just a more succinct and semantic way to invoke a function that processes a string and a set of relevant values. The name of the template tag function precedes the template literal — as in the following example, where the template tag function is named print. The print function will interpolate the arguments and serialize any objects or arrays that may come up, avoiding the pesky [object Object].
Since tagged template literals are just sugar of function calls, you can re-write the above as an equivalent function call:
This may be reminiscent of the console.log-style interpolation:
You can see how the tagged template reads more naturally than a traditional "formatter" function, where the variables and the template itself have to be declared separately.
Special Characters
Character | Meaning |
---|---|
\0 | Null Byte |
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Tab |
\v | Vertical Tab |
\' | Apostrophy |
\" | Double Quote |
\XXX | The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol. |
\xXX | The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol. |
\uXXXX | The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. |
\u{XXXXX} | Unicode code point escapes. For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04. |
Special Characters
You can insert a quotation mark inside a string by preceding it with a backslash. This is known as escaping the quotation mark. For example:
const quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."; console.log(quote);
To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path c:\temp to a string, use the following:
const home = "c:\\temp";
You can also escape line breaks by preceding them with backslash. The backslash and line break are both removed from the value of the string.
const str = "this string \ is broken \ across multiple \ lines."; console.log(str); // this string is broken across multiple lines.