# Javascript

* [good visual explanation of JS (event loop, hoisting, scope, promise, ...)](https://dev.to/lydiahallie/series/3341)
* [Doka. Documentation about JS, HTML, CSS with easy language](https://doka.guide/)

## 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

```javascript
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

### [Number](https://javascript.info/types#number)

```javascript
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`.

### [BigInt](https://javascript.info/types#bigint-type)

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

### [String](https://javascript.info/types#string)

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

### [Boolean (logical type)](https://javascript.info/types#boolean-logical-type)

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

### [The “null” value](https://javascript.info/types#the-null-value)

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:

```javascript
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 “undefined” value](https://javascript.info/types#the-undefined-value)

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`:

```javascript
let age;

alert(age); // shows "undefined"
```

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

```javascript
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.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://amartyushov.gitbook.io/tech/programming-languages/javascript-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
