Reassignable
var name;
*global
let name
*used for local scope vars. Index in a loop or joining string together.
const name
*not reassignable
`${}`
*Template string. It pre-formats partially any string in console, alert, doc.write, etc… Vars inside it stay in
${var}
let john = [“masc”, “20”, “bv”];
let mark = [“masc”, “40”, “it”];
let all = […john, “fem”, “18”, “us”, …mark]
*Spread operator. It inserts arrays’ values in an array
new Map();
let person = new Map();
person.set(“name”, “albano”); console.log(person);
*to create a map object. It’s a key-value object type. Keys can be functions, arrays, etc. any primitive.
person.get(“name”)
*gets a key from it
Person.has(“key”)
*returns true or false. If it has or not a key
Person.keys() ou .values();
*returns all the keys ou values. And we can iterate through each key or value
Person.forEach(function(person){
Console.log(person)
})
*to manipulate its elements
let person = new Map([
[“sth”],
[new Date(), “yesterday”]
]);
*we can initialize it in the constructor
let numbers = [1, 2, 3, 4, 4, 5, 6, 1, 2, 7, 8]//repeated 1, 2 4’ll be ignored
let numSet = new Set(number);
ou
numSet.add(“john”);
numSet.add(“paul”);
*to declare and populate a set
numSet.has(“john”)
*returns true or false whether it has a value or not
numSet.delete(“paul”) vs. numSet.clear()
*to delete a value vs. to empty the set
numSet.size
*we get the number size
We can manipulate wit forEach too
For loops in js are for iterable objects, string, arrays, nodelist, Map (also .keys() or .values()), Sets. To get
each element of those:
let langs = [“javascript”, “python”]
for (let x of langs){
console.log(x)}
*x is storing the subsequent values of langs. To manipulate them.
let person = {
name(){ return “Albano”} ou name: function()…,
age(){ return “Futa” ou age: function()..}
console.log(person.name());
*pass function as object elements
() => {}
Let personsName = name => console.log(name)//’cuz we’ve only 1 parameter and code line
personsName(“Dário”);
*declare arrow functions
let [first, last] = [“Javascript”, “Java”, “Ruby”, “Python”];
let [first, , , last] = [“Javascript”, “Java”, “Ruby”, “Python”]; // last gets python
*for distructuring assignment. First gets javascript and last gets java, etc…
function* myFunc(i){
yield i;
yield i + 5;}
let gen = myFunc(5);
console.log(gen.next()); //runs yield i
console.log(gen.next());//runs yield i + 5
*for generators function
class person{
constructor(name, age, profession){
this.name = name;
this.age = age;
this.profession = profession};
let alfred = new person(“Alfred”, 30, “carpainter”);
console.log(alfred);
*to declare a class. With one constructor function name, which serves to create and initialize class objs
class student extends person{
constructor(name, age, prof, school, grade){
super(name, age, prof);
this.school = school;
this.grade = grade;
}}
let louis = new student (“john”, 20, “developer”, “ITEL”, “13ª”);
console.log(louis);
*for class inheritance
class student extends person{
constructor(name, age, prof, school, grade){
super(name, age, prof);
this.school = school;
this.grade = grade;
getName(){return this.name;}
setName(name){this.name = name}
getSchool (){return this.school;}
setSchool (school){this.school = school}
*to set the getters and setters of the class. Are just simple methods
Let promise = Promise.resolve(“You finished all: Yes”);
promise.then(()=>{ console.log(“You fulfilled it all!”)})
ou
let myPromise = new Promise((resolve, reject) =>{
let finishedWork = true;
if (finishedWork){
resolved();
}else {reject()}
});
myPromise.then(()=>{ console.log(“Homeword done, goooog!”) })
*to declare promises. If a var is true do sth. And then do something. Used to retrieve the datas from
servers
myPromise.then(…).catch(()=>{console.log(“Task not done yet!”)})
*to catch the uncaught error/do sth, whether promise is incomplete
fetch(url).then()
fetch(“https:/fsdggf”).then(res => res.json()).then(console.log)
*to fetch data from server, simpler than Promise. Json() converts the response data into json.