Javascript

Overview

There is no any preparation or compilation of JS.

JS can be executed not necessarily in browser but anywhere where there is a JS engine:

  • V8 (Chrome, Opera, Edge)

  • SpiderMonkey (Firefox)

Engine parses the code -> converts to machine code -> the code is executed

"use strict" or 'use strict' always in the beginning of the file (So, for now "use strict"; is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.)

Syntax

Variables

let user = 'John'; // define variable, can be reassigned
const city = 'Paris'; // can not be reassigned
  • Variables named apple and APPLE are two different variables.

  • The name must contain only letters, digits, or the symbols $ and _.

  • The first character must not be a digit.

Data types

dynamically typed language

let n = 123;
n = 12.345;

The number type represents both integer and floating point numbers.

There are many operations for numbers, e.g. multiplication *, division /, addition +, subtraction -, and so on.

Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity, -Infinity and NaN.

In JavaScript, the “number” type cannot safely represent integer values larger than (253-1) (that’s 9007199254740991), or less than -(253-1) for negatives.

let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed another ${str}`;

The boolean type has only two values: true and false.

The special null value does not belong to any of the types described above.

It forms a separate type of its own which contains only the null value:

let age = null;
typeof null // "object" This is known bug of typeof

In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents “nothing”, “empty” or “value unknown”.

The special value undefined also stands apart. It makes a type of its own, just like null.

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned, then its value is undefined:

let age;

alert(age); // shows "undefined"

Technically, it is possible to explicitly assign undefined to a variable:

let age = 100;

// change the value to undefined
age = undefined;

alert(age); // "undefined"

…But we don’t recommend doing that. Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.

Symbol

Object

Type conversion

String Conversion – Occurs when we output something. Can be performed with String(value). The conversion to string is usually obvious for primitive values.

Numeric Conversion – Occurs in math operations. Can be performed with Number(value).

The conversion follows the rules:

Value
Becomes…

undefined

NaN

null

0

true / false

1 / 0

string

The string is read “as is”, whitespaces (includes spaces, tabs , newlines etc.) from both sides are ignored. An empty string becomes 0. An error gives NaN.

Boolean Conversion – Occurs in logical operations. Can be performed with Boolean(value).

Follows the rules:

Value
Becomes…

0, null, undefined, NaN, ""

false

any other value

true

Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:

  • undefined is NaN as a number, not 0.

  • "0" and space-only strings like " " are true as a boolean.

Comparison

  • Treat any comparison with undefined/null except the strict equality === with exceptional care.

  • Don’t use comparisons >= > < <= with a variable which may be null/undefined, unless you’re really sure of what you’re doing. If a variable can have these values, check for them separately.

Last updated

Was this helpful?