Introduction

Console

  • console is an object, i.e., a collection of data and actions.

  • console.log() is similar to Python's print().

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

/*
multi
line
comment
*/

Data Types

Type
Description
Example

Number

Integers and floats.

4, 23.4

Bigint

Greater than 2^53-1 or less than -(2^53-1)

1234567890123456n

String

Anything enclosed in single or double quotes

"Hello!"

Boolean

Has only two values

True,False

Null

Intentional absence of a value

null

Undefined

A given value does not exist

undefined

Symbol

Unique IDs

Objects

Collections of related data

All but objects are considered primitive data types.

Properties

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

Methods

Methods are actions we can perform.

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

Built-in Objects

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

Variables

Declaration

const and let is the preferred way to declare variables. var is deprecated.

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.

let meal = 'Toast'
meal = 'Fries'

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

const indicates a constant and cannot be changed (TypeError) or declared empty (SyntaxError).

Operators

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

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

String Interpolation

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

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

typeof

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

Last updated