Skip to content

Latest commit

 

History

History
155 lines (111 loc) · 6.27 KB

lecture-05.md

File metadata and controls

155 lines (111 loc) · 6.27 KB

Introduction to JavaScript

This lecture provides an introduction to JavaScript. It will introduce the foundations of the language, its various data types and control structures.

Variables

A "variable" is a place where you can store information, such as a string, or a number. New variables in JavaScript are declared using one of three keywords: let, const, or var.

let and const

Types

All variables have a type. In our example above, the variable x is a number. JavaScript supports the following types:

  • String, e.g. "Hello World"
  • Number, e.g. 5
  • Float, e.g. 10.6
  • Boolean, e.g. true or false
  • Array*, e.g. [1, 2, 3] or ['what', 'is', 'your', 'name']
  • Object, e.g. {name: 'Jenny', age: 24}, or the special object null
  • Function, e.g. function () { return 4; }

In addition, a variable may be undefined. This is also a special type.

Null & undefined

The values null and undefined are very similar in JavaScript, but they behave a bit differently. The difference is that null always has type "object", and undefined always has type "undefined".

Whenever you declare a variable, but you don't set a value, the variable will become undefined. JavaScript will never make a variable null unless you explicitly program it.

let x;
console.log(typeof x); // -> 'undefined'

Number

All numbers in JavaScript are considered numbers with or without decimal

Array

Variables that are arrays contain a list of things, instead of just one thing. What's inside the array, we typically call "elements". So, the array [1, 2, 3] has three elements. The array [] has no elements and is therefore empty. The number of elements in an array is called its "length".

When you want to access an element inside an array, you use an "index". This is the number that you put between brackets ([]).

Given the following code:

var arr = ['john', 'jane', 'jack'];
console.log(arr[0]);

The number 0 is the "index of the first element of array arr". Conversely, the element "at index 0 in array arr is 'john'".

Instead of a number, you can also use a variable to access elements in an array, as long as this variable is a number:

let arr = ['john', 'jane', 'jack'];
let a = 1;
console.log(arr[a]); // -> jane

If the index you use is not an integer (a whole number), or if it's less than 0 or if it's greater than or equal to the array's length, you will get back undefined.

Comparison operators

Note the two different uses of the equals sign: A single equals sign (=) is used to assign a value to a variable. A triple equals sign (===) is used to compare two values (see Equality Operators).

Equality operators

  • Equality ==
  • Inequality !=
  • Identity / strict equality ===
  • Non-identity / strict inequality !==

How does this work in practice?

1 == 1; // -> true
7 == '7'; // -> true
1 != 2; // -> true
5 === 5; // -> true
9 === '9'; // -> false
3 !== 3; // -> false
3 !== '3'; // -> true

Relational operators

  • Greater than operator >
  • Greater than or equal operator >=
  • Less than operator <
  • Less than or equal operator <=
4 > 3; // -> true
3 >= 3; // -> true
13 < 12; // -> false
3 <= 4; // -> true

Arithmetic operators

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Remainder (sometimes called modulo) %

    Returns the remainder left over after you've shared the left number out into a number of integer portions equal to the right number.
8 + 9; // -> 17, adds two numbers together.
20 - 12; // -> 8, subtracts the right number from the left.
3 * 4; // -> 12, multiplies two numbers together.
10 / 5; // -> 2, divides the left number by the right.
8 % 3; /// -> 2, as three goes into 8 twice, leaving 2 left over.

-Arithmetic_Operators

Asynchronous JavaScript: Callbacks and Promises

A callback is a function that will be executed when an asynchronous operation has been completed.

A callback is passed as an argument to an asynchronous operation. Normally, this is passed as the last argument of the function. Doing this it’s a good practice, so keep that in mind.

Reading