Results: 62
Concatenate strings and variables
Variable
myName
will be concatenated with the string
let myName = 'Valeri';
console.log('My name is ' + myName + '.');
The result will be:
My name is Valeri.
Simple Math calculations
Addition of two integers
5+5; // 10
Multiplication of two integers
5*5; // 25
If we add a number to a string, the number will be converted into a string and the result will be a concatenation of the two strings
5+'12'; // 512
String multiplied by an integer
5*'5'; // 25
Multiplication of a string (but not numeric string) and a number
5*'t'; // NaN
The first operation of the expression is addition and then concatenation
5+5+'2'; // 102
Make an object from a string
function mapString(string) {
  let map = {};
  for (let i = 0; i < string.length; i++) {
    let letter = string[i];
    if (map[letter]) {
      map[letter].push(i);
    } else {
      map[letter] = [i];
    }
  }
  return map;
}
console.log(mapString('blblllaaha'))
How FOR loop works
In the example code below the loop flows through these steps: Setting initial variable value of i to 0 Testing if the loop should be running while i is 0 Running the code block console.log(i) Updating i to be 1 Testing if the loop should be running while i is 1 Running the code block console.log(i) Updating i to be 2 Testing if the loop should be running while i is 2 This means the code will log 0 and 1 to the console.
for (let i = 0; i < 2; i++) {
   console​.log(i)
}
falsy values
In JavaScript, there are 6 falsy values:
1. false: the boolean value false
2. 0: the number zero
3. '': the empty string, a string with no characters
4. NaN : stands for "Not a Number", usually caused by math errors
5. undefined: a variable's value before it is assigned a value
6. null: a blank value that can be assigned to a variable
Declare a variable that will be use later to return
In many functions, it is useful to declare a variable for the function to later return. For example:
function calculateSum(numberArray) {
    let sum = 0;
    for (let num of numberArray) {
        sum += num;
    }
    return sum;
}
return false if any member is greater than 4
The following function will return false if any number in an array of numbers is greater than 4. Otherwise, the function will return true
function lessThanFive(numbers) {
  for (let number of numbers) {
    if (number > 4) {
      return false;
    }
  }
  return true;
}
Multiple return in a function
It can be useful to have multiple returns in a function declaration as long as only 1 return is expected to run. For example:
function invert(x) {
  if (x === 0) {
     return 'input was zero';
  } else {
    return 1 / x;
  }
}
The function will still work using an if statement instead of an if...else statement
function invert(x) {
  if (x === 0) {
     return 'input was zero';
  }
  return 1 / x;
}
Affects only the first element with the specified class
.slide
document.querySelector('.slide').remove()
Removes all the elements with the specified class
document.querySelectorAll('.slide').remove()
Fetch with promises
async function start() {
  try {
    fetch("https://dog.ceo/api/breeds/list/all").then(function(response) {
      return response.json()
    }).then(function(data) {
      createBreedList(data.message)
    })
  } catch (e) {
    console.log("There was a problem fetching the breed list.")
  }
}
Fetch with
async
&
await
keywords
async function start() {
  try {
    const response = await fetch("https://dog.ceo/api/breeds/list/all")
    const data = await response.json()
    createBreedList(data.message)
  } catch (e) {
    console.log("There was a problem fetching the breed list.")
  }
}
Results: 62