Show Menu
Cheatography

haodt-javascript-cheatsheet Cheat Sheet by

Cheat sheet for javascript basic to advance by HaoDT

Blog

Boolean

Type conversion
Tự động ép kiểu
Type coercion
Ép kiểu ngầm
Truthy
Falsy
Logical operators

Number

Number.pa­rse­Int­('1.5')
1
Number.parseFloat('1.5')
1.5 => cả parseInt và parseFloat luôn parse từ trái sang phải đến khi có lỗi
n.toFixed
n.toPr­ecision
Làm tròn đến chữ số có nghĩa (bao gồm cả số nguyên và thập phân)

Math

Math.PI
Math.SQRT2
Căn bậc 2
Math.c­eil(x)
Làm tròn lên
Math.f­loor(x)
làm tròn xuống
Math.r­ound(x)
Làm tròn như bình thường
Math.t­runc(x)
Lấy nguyên bỏ thập phân
Math.r­andom()
Random số thực từ 0->1
Math.a­bs(x)
Math.p­ow(x, y)
Math.s­qrt(x)
Số thực không so sánh bằng được

Object

delete key
delete studen­t.name
property value shorthand
{ id: id} => { id }
in
'name' in student
clone object
Object.assign hoặc ... (spread opertator)
Clone chỉ 1 tầng, con của object không có clone ra => Nên sử dụng thư viện cho clone
Objec.keys
for ... in (duyệt key)

ES6

strict mode
sử dụng ES5 hoặc không
hoisting
đem var và định nghĩa hàm (chỉ function declar­ation không phải arrow function) lên đầu file
IIFE
thực hiện function ngay sau khi định nghĩa
closures
hàm con sử dụng biến của hàm con ngay cả khi hàm cha đã kết thúc
Rest parameter
dấu ...
Arrow function
() => {}
Curry function
hàm return về 1 function khác
Enhand Object Property
Property shorthand
{ id: id } => { id }
computed property name
[] Ex: studen­t['­name']
methor property
destru­cturing
rút trích property
Object.assign
this
call
apply
bind
bind this cho 1 object cụ thể
Protocol
iteration protocol
Chuẩn cho for ... of
 

String

Dấu backticks
`My ${name}`
String sinh ra đã cố định không thay đổi được giá trị
text.l­ength
.toLow­erC­ase()
.toUpp­erC­ase()
.indexOf
.lastI­ndexOf
.startWith
.includes
.endsWith
.slice­(start, end)
.subst­rin­g(s­tart, end)
.replace
.repla­ceAll
.split()
.join()
Kí tự ellipsis '...' => \u2026

Array

Array.i­sA­rra­y(arr)
Tồn tại phần tử
every(­cal­lback)
some (callback)
indexOf
lastIn­dexOf
includes
find
findIndex
Thêm/Xóa phần tử
push
pop
shift
unshift
slice
Duyệt mảng
.forEach
for of
map
filter
slice
reduce
fill
join
concat
reverse
sort

Lập trình bất đồng bộ

setTimeout
lưu ý với this
debounce
Đợi cho đến khi không còn gọi nó nữa thì thực hiện
throttle
Trong bao nhiêu milisecond sẽ chỉ trigger 1 lần
setInt­erval
EventLoop
Quan trọng
Ajax
XMLHtt­pRe­quest
 
RestApi
Promise
Fix callback hell
 
method
Promise chaining
.then nhiều lần
Fetch
CSP
Content Security Policy
CORS
Cross-­Ỏigin Resource Sharing
Async function
Top level await
JSON
JSON.p­ars­e(text)
parse text thành biến js (có một số trường hợp bị lỗi)
JSON.s­tri­ngi­fy(­json)
parse biến js thành text
 

ESMAScript 2016 => 2022

2016
includes
**
Lũy thừa
2017
Async function
Shared memory and atomics
String padding
padStart, padEnd
Object.value
Lấy value của object => array
Object.keys
Lấy keys => array
Object.en­tries
Lấy cả key và value
Object.getOwnPropertyDescription
Trailing Commas
2018
Rest/Spread properties for object
dấu ...
for await of
finally()
Regular expression
2019
flat, flatMap
hạ cấp mảng
Object.fr­omE­ntries
trimStart, trimEnd
Optional catch binding
catch không cần truyền param error
sort()
Giữ thứ tự những phần tử giống nhau
toString()
2020
Thêm kiểu dữ liệu BigInt
Dynamic import
import trong function lúc nào xài nó không phải import trên đầu
Nullish coalescing
?? null và undefined mới tính lấy cái sau
optional chaining
?. Ex: studen­t?.name
globalThis
Module namespace export
Thay vì import * rồi export thì giờ có thể export trên 1 dòng
for in
import.meta
2021
Logical asignment
||= &&= ??=
Numberic operator
1_000_000 nhìn dễ hơn
replaceAll
promis­e.any
2022
Top-level await
private class
Object.hasOwn

API và Postman

Json Server
cung cấp database dạng json sẽ tự tạo được api
Browser
global object là window

Web APIs

Browser URL
window.lo­cat­ion.href
có thể redirect và có thể back lại trang trước đó => recomment sử dụng lấy url hiện tại
window.location.replace
redirect nhưng không back lại được trang trước đó
window.lo­cat­ion.assign
di chuyển qua trang mới, recomment sử dụng cái này không sử dụng href
Console API
Log variations
log, info, debug, warn, error
Multiple values
Với string ``:

+ %o => object

+ %s => string

+ %i => integer

+ %f => float
Assert
show message khi condition là false
Counting
Track time
time, timeLog, timeEnd
Groups
group, groupEnd, groupC­oll­apsed
Stack trace
Tables
Styles
sử dụng %c
Clear
Storage API
Storage Interface
getItem, setItem, remove­Item, clear
 
- LocalS­torage, Sessio­nSt­orage, hay cookie,… phải parse string rồi mới lưu
- LocalS­torage => đóng tab hay đóng trình duyệt vẫn còn
- Sessio­nSt­orage => đóng tab sẽ mất
Browser Network
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          AngularJS Cheat Sheet
          JavaScript Cheat Sheet
          Web Programming Cheat Sheet

          More Cheat Sheets by tronghao

          haodt-module Cheat Sheet
          HaoDT-AWS Cheat Sheet
          haodt-lodash Cheat Sheet