A re-introduction to Java​Script Notes

Overview

Types

Numbers

Strings

var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and not ${2 * a + b}.`);
// Fifteen is 15 and not 20.

Variables

An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have a scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function. However, starting with ECMAScript 2015, let and const declarations allow you to create block-scoped variables.

Objects

// Object creation examples
let foo = {};

let bar = new Object();

let koray = {
    name: 'Koray',
    age: 34,
    children: [{
        name: 'Deniz',
        age: 2
    }, {
        name: 'Toprak',
        age: 2
    }]
};

Object Prototypes

function Person(name, age) {
    this.name = name;
    this.age = age;
}

let koray = new Person('Koray', 34);

console.log(koray);
console.log(koray['name']);
console.log(koray.age);

Arrays

let myArr = ['foo', 'bar'];
myArr.push('baz');

for (let val of myArr) {
    console.log(val);
}

// Since ES5
myArr.forEach(function(val, index, array) {
    // Do something with currentValue or array[index]
});
function f(firstName, lastName) {
    console.log(firstName.concat(' ').concat(lastName));
}

f(...['Koray', 'Tugay']);  // Spreading values in the array

Functions

function f() {
    console.log(arguments);
}

f(1, 2, 3);  // [Arguments] { '0': 1, '1': 2, '2': 3 }
function f(...args) {
    console.log(args);
}

f(1, 2, 3);  // [ 1, 2, 3 ]
function f(...args) {
    console.log(args);
}

f.apply(null, [1, 2, 3]);  // [ 1, 2, 3 ]
let a = 5;

(
    function () {
        let a = 3;
        console.log(a);
    }()
);

console.log(a);

// 3
// 5

Custom Objects

Example and this

function makePerson(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName,
        fullName: function () {
            return this.firstName.concat(' ').concat(this.lastName);
        }
    };
}

let koray = makePerson('koray', 'tugay');
console.log(koray.fullName());

// This will throw TypeError in runtime
let fullNameFunction = koray.fullName;
fullNameFunction();
// Cannot read property 'concat' of undefined

Constructor Functions

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;

    this.fullName = function () {
        return this.firstName + ' ' + this.lastName;
    }
}

let koray = new Person('koray', 'tugay');
console.log(koray.fullName());

Prototypes

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

Person.prototype.fullName = function () {
    return this.firstName + ' ' + this.lastName;
};


let koray = new Person('koray', 'tugay');
console.log(koray.fullName());

Closures

function adderBuilder(foo) {
    return function (baz) {
        return foo + baz;
    }
}

let myAdder = adderBuilder(5);

console.log(myAdder(10));  // Prints 15