ES6-string-injection
Di ES6 Ada cara mudah memasukan variable ke dalam String. Kalian cukup menggunakan tanda`Put your String here and add your variable like this ${yourVariable}`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var note = notes.addNote(argv.title, argv.body); | |
if(note){ | |
console.log("successfully saving a new note"); | |
console.log("____"); | |
//ES6 javascript string injection | |
console.log(`Title: ${note.title}`); | |
console.log(`Body: ${note.body}`) | |
}else{ | |
console.log("A note with this title is already exist"); | |
} |
Variable 'let'
Perbedaan utama di variable let adalah pada scope-nya.let
syntax memungkinkan kita untuk mengimplementasi local variable dengan mudah.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doSth() { | |
//tuce is *not* visible out here | |
for( let i = 0; i < 5; i++ ) { | |
//i hanya terlihat di scope ini saja | |
} | |
//i tidak terlihat disini | |
} | |
function byE40() { | |
//variable i2 terlihat disini | |
for( var i2 = 0; i2 < 5; i2++ ) { | |
//var i2 is visible to the whole function | |
} | |
//dan juga disini | |
} |
Arrow Function
Arrow function sangat memudahkan kita menulis code di javascript. Tapi penggunaan arrow function saya sarankan hanya seandainya kamu sudah tau menulis fungsi tanpa menggunakan arrow function. karena penggunaan arrow function akan membingungkan tanpa memahami proses apa yang terjadi di balik itu.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var square = (x) => { | |
var result = x*x; | |
return result; | |
}; | |
//All can be simplified into one line | |
//without curly braces means you need the expression to be returned. | |
var autoReturn = (x) => x * x ; | |
var withoutReturnValue = (x) => { | |
console.log(x*x); | |
}; | |
//With only one argument you can get rid of the prenthesis | |
var oneArgument = x => x * x; |
Object destructuring
Kalian bisa membuat variable baru dari objects' properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var user = { | |
name: 'Peter', | |
age: 26, | |
}; | |
// create a new variable name from user object. | |
var {name} = user; | |
// with this, you can do such thing like | |
const {MongoClient, ObjectID} = require('mongodb'); | |
// instead of | |
const MongoClient = require('mongodb').MongoClient; | |
const ObjectID = require('mongodb').ObjectID; |
Promises
Untuk menghandle Asynchronous call, kita bisa menggunakan Promise methode. Cara ini lebih baik karena bisa meminimalisir kesalahan ketika berkerja dengan asynchronous methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// initiating a new instance of Promise | |
// using setTimeout to simulate delay | |
var asyncAdd = (a, b) => { | |
return new Promise((resolve, reject) =>{ | |
setTimeout(() => { | |
if(typeof a === 'number' && typeof b === 'number'){ | |
resolve(a + b); | |
}else{ | |
reject('Arguments must be numbers'); | |
} | |
}, 1500) | |
}); | |
}; | |
//The then method will be called dependent to the result of the somePromise. 1st argument | |
//will only be called if resolve is called and the 2nd method is called if the reject method had been called. | |
asyncAdd(5, 7).then((result) => { | |
console.log('Result: ', result); | |
}, (errorMessage) => { | |
console.log('Error: ', errorMessage); | |
}); | |
// We can also chaining two asynchronous methods like this | |
// We only provide one error handling method 'catch' | |
asyncAdd(5, "7").then((result) => { | |
console.log('Result: ', result); | |
return asyncAdd(result, 33); | |
}).then((result) => { | |
console.log('Should be 45', result) | |
}).catch((errorMessage) => { | |
console.log(errorMessage); | |
}); |