Notes on JavaScript

JavaScript


Originally created to be a browser only language, JS is currently the most used, highly optimised browser language with uses in other environments as well.

There are many languages that get “transpiled” to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.

Table of Contents


Introduction


What can the in-browser JS do?
Good doc for reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

Fundamentals


To make the code run in the modern way, use "use strict";, at the beginnning.

Modern JavaScript supports “classes” and “modules” – advanced language structures (we’ll surely get to them), that enable use strict automatically. So we don’t need to add the "use strict" directive, if we use them.

Basics

/* -- let --*/
let user = 'John', age = 25, message = 'Hello';

/* -- const --*/
const COLOR_RED = "#F00";
const COLOR_GREEN = "#0F0";
const COLOR_BLUE = "#00F";
const COLOR_ORANGE = "#FF7F00";

// ...when we need to pick a color
let color = COLOR_ORANGE;
alert(color); // #FF7F00
alert(Infinity); // Infinity

// null
let age = null;
age = 100;

// change the value to undefined
age = undefined;
alert(age); // "undefined"

// the "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;

Interactions

Type Conversions

Things similar to C++

Nullish coalescing operator ??

Label based breaking

outer: for (let i = 0; i < 3; i++) {  for (let j = 0; j < 3; j++) {    let input = prompt(`Value at coords (${i},${j})`, '');    // if an empty string or canceled,  // then break out of both loops   if (!input) break outer; // (*)    // do something with the value...
  }
}
alert('Done!');

We can also move the label to a different line:

outer:
for (let i = 0; i < 3; i++) { ... }

However, this doesn't allow you the freedom to jump anywhere from anywhere unlike goto command in C.

Functions


A function declaration looks like this:

function name(parameters, delimited, by, comma) {
  /* code */
  return 100; // optional ofcourse
}
// Function Declaration
function sum(a, b) {
  return a + b;
}
// Function Expression
let sum = function(a, b) {
  return a + b;
};

Callback functions

function ask(question, yes, no) {
  if (confirm(question)) yes()
  else no();
}

function showOk() {
  alert( "You agreed." );
}

function showCancel() {
  alert( "You canceled the execution." );
}

// usage: functions showOk, showCancel are passed 
// as arguments to ask
ask("Do you agree?", showOk, showCancel);

Arrow functions

The jorney from functions to arrow functions:

1. var anon = function add(a, b) { return a + b };
2. var anon = (a, b) => a + b;
3. var anon = (a, b) => { return a + b };
4. var anon = a => a; // if we have only one parameter
5. var () => {} // noop

// this looks pretty nice when you change something like:
[1,2,3,4].filter(function (value) {return value % 2 === 0});
// to:
[1,2,3,4].filter(value => value % 2 === 0);

JavaScript Specials

let userName = prompt("Your name?", 2);
alert( "Visitor: " + userName );
// expression at the right side
1. let sum = (a, b) => a + b;

// or multi-line syntax with { ... }, need return here:
2. let sum = (a, b) => {   // ...   return a + b;
    }

// without arguments
3. let sayHi = () => alert("Hello");

// with a single argument
4. let double = n => n * 2;

Objects


Objects are associative arrays with several special features.

They store properties (key-value pairs), where:

To access a property, we can use:

Additional operators:

What we’ve studied in this chapter is called a “plain object”, or just Object.

There are many other kinds of objects in JavaScript:

Copying an Object

Well, objects are copied as reference.

Object.assign () servers to produce a clone of one or several objects combined.

Object methods

// Method 1
let user = {
  name: "John",
  age: 30
};

user.sayHi = function() {
  alert("Hello!");
};


// Method 2
let user = {
  name: "John",
  age: 30
};

function sayHi() {
  alert("Hello!");
};

user.sayHi = sayHi;


// Method 3
user = {
  sayHi: function() {   alert("Hello");
  }
};


// Method 4
user = {
  sayHi() {   alert("Hello");
  }
};

Keyword: this

Constructor and new

function User(name) 
{  this.name = name;   this.sayHi = function() {    alert( "My name is: " + this.name );
    };
}

let john = new User("John");

Optional Chaining using ?.

The optional chaining ?. syntax has three forms:

  1. obj?.prop – returns obj.prop if obj exists, otherwise undefined.
  2. obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
  3. obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.

Conversions

obj.toString() can be used to convert an object to a string.