Dorokhov.codes

01. Syntax and standards

// A variable is a symbolic name for a value.
// Variables are declared with the let keyword:
let x;      // Declare a variable named x.

// Values can be assigned to variables with an = sign
x = 0;      // Now the variable x has the value 0
x           // => 0: A variable evaluates to its value.

// JavaScript supports several types of values
x = 1;              // Numbers.
x = 0.01;           // Numbers can be integers or reals.
x = "hello world";  // Strings of text in quotation marks.
x = 'JavaScript';   // Single quote marks also delimit strings.
x = true;           // A Boolean value.
x = false;          // The other Boolean value.
x = null;           // Null is a special value that means "no value."
x = undefined;      // Undefined is another special value like null.
// JavaScript's most important datatype is the object.
// An object is a collection of name/value pairs, or a string to value map.
let book = {                // Objects are enclosed in curly braces.
    topic: "JavaScript",    // The property "topic" has value "JavaScript."
    edition: 7              // The property "edition" has value 7
};                          // The curly brace marks the end of the object.

// Access the properties of an object with . or []:
book.topic                  // => "JavaScript"
book["edition"]             // => 7: another way to access property values.
book.author = "Flanagan";   // Create new properties by assignment.
book.contents = {};         // {} is an empty object with no properties.

// Conditionally access properties with ?. (ES2020):
book.contents?.ch01?.sect1  // => undefined: book.contents has no ch01 property.
// JavaScript also supports arrays (numerically indexed lists) of values:
let primes = [2, 3, 5, 7]; // An array of 4 values, delimited with [ and ].
primes[0]                   // => 2: the first element (index 0) of the array.
primes.length               // => 4: how many elements in the array.
primes[primes.length-1]     // => 7: the last element of the array.
primes[4] = 9;              // Add a new element by assignment.
primes[4] = 11;             // Or alter an existing element by assignment.
let empty = [];             // [] is an empty array with no elements.
empty.length                // => 0

// Arrays and objects can hold other arrays and objects:
let points = [          // An array with 2 elements.
    {x: 0, y: 0},       // Each element is an object.
    {x: 1, y: 1}
];

let data = {                // An object with 2 properties
    trial1: [[1,2], [3,4]], // The value of each property is an array.
    trial2: [[2,3], [4,5]]  // The elements of the arrays are arrays.
};
// Constants:
const number = 5;

Old syntax

Scripts < ES6:

var name = "Andrew";

We can even create vars this way:

name = "Andrew";

Difference between let and var:

// This code won't throw an error (it will write "undefined").
// This behavior called hoisting (https://www.w3schools.com/js/js_hoisting.asp).
// Hoisting is JavaScript's default behavior of moving declarations to the top.
console.log(name);
var name = "Andrew";

// This code will throw an error:
console.log(name);
let name = "Andrew";
// It works:
{
   var name = "Andrew";
}
console.log(name);

// It doesn't work:
{
   let name = "Andrew"; // or const
}
console.log(name);

Standards

ECMAScript (ES) - the standardized version of the JavaScript language.

Since ES6, the ECMAScript specification has moved to a yearly release cadence, and versions of the language (ES2016, ES2017, ES2018, ES2019…).

Standard Year Features Comment
ES5 2009 For most of the 2010s, version 5 of the ECMAScript standard has been supported by all web browsers.
ES6 2015 Class and module syntax, let/const, arrow functions, template literals, promises, destructuring, default parameters. A major update that introduced modern JavaScript features, setting the foundation for modern web development.
ES2017 2017 Async/await, Object.entries(), Object.values(), shared memory, Atomics. Simplified asynchronous programming with async/await, widely adopted in modern JavaScript codebases.
ES2018 2018 Rest/spread properties for objects, asynchronous iteration, Promise.finally(). Enhanced syntax and functionality for handling objects and asynchronous code.
ES2019 2019 Array.flat(), Array.flatMap(), Object.fromEntries(), optional catch binding, String.prototype.trimStart/trimEnd. Focused on improving developer ergonomics with small but impactful enhancements.
ES2020 2020 BigInt, optional chaining (?.), nullish coalescing operator (??), dynamic import(), Promise.allSettled(). Introduced features to simplify dealing with numbers, deep object access, and promises.

How to tell a script what standard we use

We are using modern standards (strict mode):

// We are specifying it at the beginning of a file or a function.
"use strict";

Nullish coalescing operator

The nullish coalescing operator (??) is a logical operator in JavaScript that returns the right-hand operand when the left-hand operand is either null or undefined. It is useful for providing default values without mistakenly treating other falsy values like 0, false, or "" as null or undefined.

let count = 0;

// Using ||
let result1 = count || 5; // Output: 5 (0 is falsy)

// Using ??
let result2 = count ?? 5; // Output: 0 (0 is not null or undefined)

Optional chaining operator

The optional chaining operator (?.) in JavaScript provides a safe way to access properties, methods, or elements of an object or array that might be null or undefined. Returns undefined if the accessed part of the chain does not exist.

const obj = { a: { b: 42 } };
console.log(obj.a?.b); // 42
console.log(obj.a?.c); // undefined

const obj = { greet: () => "Hello!" };
console.log(obj.greet?.()); // "Hello!"
console.log(obj.nonExistentMethod?.()); // undefined

const arr = [1, 2, 3];
console.log(arr?.[1]); // 2
console.log(arr?.[5]); // undefined

Tools

Can I use - browser support tables for modern web technologies.