- Using
//for 1 line comment,/* */for multi-line comments - JS can be execute without
;but it’s minified need to be; - Variable and function name are case sensitive
- Variables/functions should be names with format camel case (
nameOfVariable)- Name a constant:
const FAV_PET = 'Cat' UpperCamelCaseshould be used by conversion for ES6 class names.
- Name a constant:
- Using
\for special characters, for example\"for"inside double quote
Open the browser (I use Brave), press F12 to open Inspect window, then choose tab Console. Now you can practice on this console window, for example, try with console.log("Hello World") end press Enter
- “ES” = “ECMScript” ~ “Javascript”
- Most of browser use ES6 but not all
- ES6 = ES2015
- New feature:
Arrow Function,Classes,Modules,Promise,Generator,letandconst - Read more about ES on w3schools.
// Concise Object Literal Declarationconst getMousePosition = (x, y) => ({ x, y });// Concise Declarative Functionsconst persons = { name: 'Nam Nguyen', sayHello() { return `Hello. My name is ${this.name}`; // Hello. My Name is Nam Nguyen }};class Book { constructor(author) { this._author = author; }
// getter get author() { return this._author; }
// setter set author(author) { this._author = author; }}
const book = new Book('anonymous');console.log(book.author); // anonymousconst add = (x, y) => x + y;const subtract = (x, y) => x - y;
export { add, subtract };
export default function (x, y) { return x + y;}import { add, subtract } from './calculator.js'; // only add, subtractimport * as everthing from './calculator.js'; // everythingimport anything from './calculator.js'; // import defaultvar name; // global scopelet age; // ES6, block scope (inside {} or function,...)const PI = 3.14; // ES6 can't be changedfunction funcName() { let nameInFuc = 'anonymous';}
console.log(nameInFuc); // undefinedDifference between var, let, const
var a = 1;var a = 2; // ok a = 2 nowa = 5; // ok a = 5 nowlet c = 1;let c = 2; // error, this variable is declaredc = 3; // ok, c = 3 nowconst b = 1;const b = 2; // errorb = 2; // errorconst s = [1, 2, 3];s = [1, 2, 3, 4]; // errors[1] = 3; // OK// print outputconsole.log('Hello World');
// print error messageconsole.error('Simple Error');
// print warning messageconsole.warn('Simple Warning');
// print a tableconsole.table({ a: 1, b: 2 });
// group content in a separate blockconsole.group('simple');console.log('Output here');console.error('Error here');console.groupEnd();||checkfalsevalues??checknullorundefined
const a = 0 || 1; // output 1const b = 0 ?? 1; // output 0const c = false || 1; // output 1const d = false ?? 1; // output falseconst e = null || 1; // output 1const f = null || 1; // output 1You don’t need use try/catch in every async/await. You only need to do it at the top level
In synchronous programming, operations are performed one after the other, in sequence. Each line of code needs to wait for the previous one to finish before proceeding to the next.
console.log('Hi');console.log('Nam');console.log('How are you?');
// output// Hi// Nam// How are you?Asynchronous programming, on the other hand, allow multiple tasks to run independently of each other. A task can be initiated and while waiting for it to complete, other tasks can proceed.
console.log('Hi');setTimeout(() => console.log('Nam'), 3000);console.log('How are you?');
// output// Hi// How are you?// Namconst data = await Promise.all([timeout(3000), timeout(2000), timeout(1000)]);// delay 1 delay 2 delay 3// ms ------ 1s ------ 2s ------- 3s// ============================== 0 delay 1// =================== 0 delay 2// ========= 0 delay 3// ============================== Promise all (executed in 3s)await timeout(3000);await timeout(2000);await timeout(1000);// -- delay 1 ------------ delay 2 ------------ delay 3// ms -- 1s ---- 2s ---- 3s ---- 4s ---- 5s ---- 6s// ===================== 0 delay 1// ===================================== 0 delay 2// ============================================= 0 delay 3// ============================================= async...await executed 6sNote: If there is no data dependency you should use Promise.all