> For the complete documentation index, see [llms.txt](https://x7331.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://x7331.gitbook.io/notes/codecademy/learn-javascript/introduction.md).

# Introduction

## Console

* `console` is an object, i.e., a collection of data and actions.
* `console.log()` is similar to Python's `print()`.

```javascript
// single line comment
console.log("Hello")

/*
multi
line
comment
*/
```

## Data Types

<table><thead><tr><th width="136">Type</th><th width="403">Description</th><th>Example</th></tr></thead><tbody><tr><td>Number</td><td>Integers and floats.</td><td><code>4</code>, <code>23.4</code></td></tr><tr><td>Bigint</td><td>Greater than 2^53-1 or less than -(2^53-1)</td><td><code>1234567890123456n</code></td></tr><tr><td>String</td><td>Anything enclosed in single or double quotes</td><td><code>"Hello!"</code></td></tr><tr><td>Boolean</td><td>Has only two values</td><td><code>True</code>,<code>False</code></td></tr><tr><td>Null</td><td>Intentional absence of a value</td><td><code>null</code></td></tr><tr><td>Undefined</td><td>A given value does not exist</td><td><code>undefined</code></td></tr><tr><td>Symbol</td><td>Unique IDs</td><td></td></tr><tr><td>Objects</td><td>Collections of related data</td><td></td></tr></tbody></table>

All but objects are considered **primitive data types**.

## Properties

```javascript
console.log('Hello'.length); // 5
```

## Methods

Methods are actions we can perform.

```javascript
console.log('hello'.toUpperCase()); // 'HELLO'
console.log('Hey'.startsWith('H')); // true
console.log('  Hey there!  '.trim()); // 'Hey there!'
```

## Built-in Objects

{% code overflow="wrap" %}

```javascript
console.log(Math.random()); // random number between 0 (inclusive) and 1 (exclusive)
console.log(Math.floor(Math.random() * 50); // rounds down the float and returns the largest int less than or equal to a given number
console.log(Math.ceil(Math.random() * 50); // rounds up the float and returns the smallest int less than or equal to a given number
```

{% endcode %}

## Variables

### Declaration

`const` and `let` is the preferred way to declare variables. `var` is deprecated.&#x20;

```javascript
var favoriteFood = 'pizza';
let numOfSlices = 8;
const rate = 5;
```

`let` signals that the variable can be reassigned a different value. We can also use `var` and `let` to declare an empty variable.

```javascript
let meal = 'Toast'
meal = 'Fries'

let price;
console.log(price); // undefined
price = 35;
```

&#x20;`const` indicates a constant and cannot be changed (`TypeError`) or declared empty (`SyntaxError`).

{% hint style="warning" %}
When a variable is declared using the `let` or `const` keywords within a block scope, it is only accessible there. If no keyword is used, it becomes a global variable by default.
{% endhint %}

### Operators

The increment (`++`) and the decrement (`--`) operators will increase and decrease the variable's value by 1, respectively.

```javascript
let a = 10;
a++;
console.log(a); // 11
```

## String Interpolation

We can interpolate variables into string using **template literals**.using backticks (`` ` ``).

```javascript
const myPet = 'dog'
console.log(`I own a pet ${myPet}.`);
```

## typeof

```javascript
const unknown = 'foo';
console.log(typeof unknown); // string
```
