What is JavaScript
In 1997, JavaScript was created to bring life to web pages, letting a developer dynamically change content and make webpage content seem less static. In JavaScript, we use scripts that can be written right in a web page’s HTML and run automatically as the page loads. JavaScript was initially a client-side scripting language, meaning that it was only used on the user’s side of the browser. This included sending forms, creating cookies, and displaying text and images.
Today, JavaScript can run not only in the browser, but also on the server. JavaScript engines like V8, allow us to create fully functional applications with servers and databases ONLY in JavaScript.
Then what’s this ES stuff?
We use ES’s standardization in order to make sure that JavaScript performs and behaves as similarly as possible across browsers. One browser might understand your JavaScript differently than another, in the same way that local colloquialisms from Haiti might not be understood the same way in Louisiana.
Currently, there are 10 versions of ES, with Ecma International planning on yearly releases. When a new version of ECMAScript comes out, JavaScript engines don’t integrate the new version immediately or even all in one go. So keep in mind that just because you can run ES10 code on one browser, doesn’t mean that it’s applicable to all of them!
What’s New In JavaScript
Const and Let
//ES5 Syntax
var myVariable = "Hello World";
//ES6 Syntax
const myVariable = "Goodbye World";
// If you tried to do something like:
myVariable = "Something Else";
// you would get an error as const is immutable
let anotherVariable = "Another One";
Arrow functions
//ES5 Syntax
function greeting(name) {
return "Hello from" + name;
}
//ES6 Syntax
const greeting = (name) => `Hello from ${name}`;
console.log(greeting("Graphem Solutions"));
//both functions return "Hello from Graphem Solutions"
const sumMyNumbers = (number1, number2) => {
let mySum = number1 + number2;
mySum = mySum + 5;
return mySum
};
console.log(sumMyNumbers(1,2))
//this returns "8"
let array = [0, 1, 2, 3, 4]
//ES5 Syntax
array = array.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
})
console.log(array)
//ES6 Syntax
array = array.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(array)
//Both of these return "10"
Destructing
//ES5 Syntax
var myObject = {
first: "item 1",
second:"item 2",
third: "item 3",
}
var first = myObject.first;
var second = myObject.second;
var third = myObject.third;
console.log(first, second, third)
//ES6 Syntax
const myObjectES6 = {
firstES6: "item 1",
secondES6:"item 2",
thirdES6: "item 3",
}
//If you'd like to have the property stored as a different variable than the property name, you can declare it with property Name:yourVariable in the brackets
let {firstES6, secondES6, thirdES6:myThird} = myObjectES6;
console.log(firstES6, secondES6, myThird)
//Both objects return "item 1 item 2 item 3"
Promises
//ES5 Syntax
firstStep(function(response) {
secondStep(response, function(responseA) {
thirdStep(nextResponse, function(responseB) {
console.log('responseA): ' + responseB);
}, error);
},error);
},error);
//ES6 Syntax
firstStep().then(function(responseA) {
return secondStep(responseA);
}).then(function(responseB) {
return thirdStep(responseB);
}).then(function(responseC) {
console.log('responseC: ' + responseC);
}).catch(error);
Classes
class Computer {
constructor(cpu, year, brand) {
this.cpu = cpu;
this.year = year;
this.brand = brand;
}
aboutComputer() {
return `My Laptop is a ${this.year} ${this.brand} and has a ${this.cpu} CPU`
}
}
//Now you can use the Computer class to create Computer objects:
let myComputer = new Computer("Intel", 2020, "MacBook Pro");
console.log(myComputer.aboutComputer());
//Returns "My Laptop is a 2020 MacBook Pro and has a Intel CPU"
Async/Await
function myEmoji() {
return new Promise(resolve => {
setTimeout(() => {
resolve('?');
}, 5000);
});
}
async function msg() {
const msg = await myEmoji();
console.log('My Emoji Is', msg);
}
msg(); // My Emoji Is: ? <-- after 5 seconds