There are several ways to create an array in Javascript:
let cities = [];
let cities = ["Paris", "Boston", "Nairobi", "Seoul"];
let cities = new Array();
let cities = new Array("Seoul","Tokyo","Bangokok");
let cities = new Array(8);
indexOf()
let cities = ["Paris", "Boston", "Nairobi", "Seoul"];
cities.indexOf("Seoul");
cities.indexOf("London");
cities.lastIndexOf("Nairobi");
array.filter() returns a new array with only elements from an original array that pass a boolean test.
filter() does not modify the original array.
let numbers = [1,2,3,4,5,6,7];
let newArray = numbers.filter(element => element > 3);
These four methods are simple ways to add and remove elements from the ends of an array.
let letters = ["j","k","l","m"];
letters.push("n");
let numbers = [1,2,3,4];
console.log(numbers.length);
In Javascript, an array can have different types of object. The same array can contain strings, integers, booleans, etc as its elements.
document.querySelector('#loginbox');
document.querySelector('.block');
document.querySelectorAll(".box");
getElementById()
let myParagraph = document.createElement('p');
myParagraph.textContent = 'I am learning Javascript DOM!';
document.querySelector('#welcomeDiv').appendChild(myParagraph);
A lot of times you want to check what type of object some variable is. Here are some Javascript methods and functions that help you know for sure what kind of variable or data you're dealing with.
let myDecision = true;
console.log(typeof myDecision);
console.log(typeof "Hello Pink!")
console.log(typeof 23523.23)
console.log(typeof 23523)
let mixedArray = [121,323,3,223.42];
console.log(typeof mixedArray)
let myMap = new Map();
console.log(typeof myMap);
let num = 5.5694235234789;
num instanceof float;
Split is a handy string method that creates an array of characters from a string
A character is a singlestring
A very handy method for rounding up a decimal to a specified number of digits.
let num = 5.5694235234789;
let n = num.toFixed(2);
A map is a type of JavaScript data structure thats used for mapping keys to values. Unlike a typical JavaScript object, where you can only map strings to values, a map allows you map different types of objects to values. Maps are considered a superior data structure to objects, and should be used for mapping keys to values.
A map also comes with some very useful methods like get(), set(), has(), keys(), entries(), clear(), delete()...
let favoriteMovies = new Map();
let favoriteMovies = new Map();
A switch statement can be used when there are many cases to compare, when looking for a match.
switch(userScore){
case 20:
gameResponse = "too low";
break;
}
case 30:
gameResponse = "try harder next time";
break;
}
case 40:
gameResponse = "you almost made it";
break;
}
case 50:
gameResponse = "good job!";
break;
}
case 60:
gameResponse = "very good!";
break;
}
Math.pow(2,3)
Math.pow(5,4)
Math.sqrt(99)
Math.sqrt(25)
Regular expressions are a type of javascript object. You can create a regex to check if a string matches a certain patter.
Reserved words are JavaScripts own words, used in the language itself, used to call data structures, loops, perform operations etc. So you're not allowed to use them to name your variables, constants, properties, or functions.
A random number generator, which includes the minimum and maximum sent into it.
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}