The Full JavaScript & ES6 Course Notes : JavaScript Language and Essentials part2

20181014
JavaScript Language and Essentials
-Syntax, statements, data types, variables, and assignment
-Functions, scope, arrays, and objects
-JS Conditions and Loops (next article)

Arrays

Arrays in javascript allows to hold multiple values within one variable.

Notice that each new element in the array is separated by a comma until we reach the end.

Often in an application we’ll want to store a list of data. Think about a To Do list for example, each one of those tasks is unique but we could represent the entire collection with an array.

Let’s explore some ways we can now interact with arrays through javascript methods:

  • Update the arrays

Let’s suppose I wanted to add a new element to the array. I could use a push method.

points.push(8);

Then we go we see six elements in our arena.

  • Removing an element

Suppose you wanted to take out the last element rather than push a new element.we can use the pop method.

var last = points.pop( );

that means remove the last element in the array and also returns that element to a variable that we assign. So let’s console.log of points now as well as last.

console.log(points, last);

  • Accessing elements

It’s very similar to how we access data through properties and objects. However we don’t have keys that map these elements to data in order to access them.

Notice that an array has a specific order so we can access elements with the array by specifying the index or order number of element that we want.

To get the first element we actually specify with the number 0 because we start counting from zero in Javascript. For this reason we call the first element with Array the zero element.

var first = point[0];

console.log(first);

Now we get 25.

*Most programming languages actually start counting from zero because of the influence from C programming languages.

  • Access the length of this array

We can actually access the length of this array very easily by calling the length property of the array.

console.log(points.length);

And there we see five elements in console.log.

Booleans and Comparison Operators

In many programming languages you’ll find a data type only shows True or false in javascript. We call this the boolean primitive data type. So this is a switch between an on and not value or a yes or no.

var a = true;

console.log(a);

If change to typeof a, we see boolean.

var a = fales;

console.log(a);

  • We can determine boolean values based on comparison operators that check for conditions

For example we have that equal to greater than and less than operators to work with the equal to operator. Looks like two equal signs that check for an equality based on the value on the left and the one on the right.

var a = 5 == 5;

console.log(a);

*Now notice that we have a single assignment operator with one equals sign here. And then the boolean operator is recognized by two equal sides right next to each other.

5 not equal 7, so it shows false.

5 is less than 7, so it’s true.

5 is not greater than 7, so it’s false.

There’s also the less than or equal to operator:

*Always put the arrow part first and the equal to part second.