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

20181018

Loops

Often in programs we want to run the same code repeatedly for a certain number of times perhaps even changing it a little bit.

Let’s say I wanted to print the numbers 1 to 10.

Now I could write separate counsel out log statements for each number starting with one end going all the way up.

console.log(1);
console.log(2);
console.log(3);

So to solve this problem in code we can write a for loop that just automatically does this for us.

  • Set a starting counting variable first
  • Set a limit to where that counting variable should stop
  • Specify by how much that counting variable should increase each time

for(var i=0; i<5; i =i+1){
console.log(i);
}

We set a limit for that counting variable and then we say by how much counting variable should increase each time.

  • i++ equals i = i+1

for(var i=0; i<5; i++){
console.log(i);
}

  • Console logging each name individually

var names =[“Frodo”, “Sam”,”Merry”, “Pippin”];
for(var i=0; i<names.length; i++){
console.log(names[i]);
}

While Loops
It just runs a code over and over again without ever stopping.

var names =[“Frodo”, “Sam”,”Merry”, “Pippin”];
var i = 0;
while(i < names.length){
console.log(names[i]);
i++;
}

Do While Loops
While Loops just runs a code over and over again without ever stopping. So it’s important while loops not to forget to increment your counting variable.

Set up while(i < 0)

Assignment

function count(x) {
var numbers = [];
if ( x % 2 == 0) { // check is even or not
for ( var i=0; i<x; i+=2){ // assign i to 0 and give it a limit of x ; and increment by 2
numbers.push(i);
}
} else {
for(var i=1; i<x i +=2) //assign i to 1 and give it a limit of x; and increment by 2
numbers.push(i);
}
}
return numbers;
}

JavaScript Language and Essentials | Section Overview

  • JavaScript runs programs based off a series of instructions called statements.
  • Keywords in JavaScript, including ‘var’, ‘if’, ‘for’, and more, are reserved for special functions.
  • A string, as a primitive data type, can represent a word or other meaningful meaningful collection of characters.
  • A number, as a primitive data type, represents real numeric values from integers to decimals.
  • Variables reserve special identifiers that point to values. JavaScript does not recognize variables that start with anything but characters a-z, the ‘$’, or the ‘_’.
  • Assignment refers to the concept of assigning values to variables. Variables
  • Operators calculate arithmetic and include those that represent addition, subtraction, multiplication, division and more.
  • Functions define a block of code that perform a specific task.
  • Objects represent a more complex type of data in JavaScript that model one or more key-value pairs.
  • Arrays hold multiple values in a list within one variable.
  • The boolean, as a primitive data type takes one of two values: true or false.
  • Comparison operators like ‘==,’ ‘<=’, or ‘>=’ determine a variable’s data and as a result, sets a resulting condition or expression to true or false.
  • If statements execute different blocks of code depending on a condition.
  • Loops such as the for-loop and while loop run the same piece of code for a designated number of times.