JavaScript Basics
Khoảng 2 phút
Data types
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {
firstName:"John",
lastName:"Doe"
};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");let, const
let age = 30;
age = 31;
// Works - Data can be changed
let now, birthYear;
now = 2025;
birthYear = 2001;
// Worksconst birthYear = 2005;
birthYear = 2001;
// Error - Data can't be changed
const name;
// Error - Should useIt's good practice to avoid variable mutation so you should use 'const', unless the variable is supposed to be changed later
You should always declare variables, avoid doing this:
lastName = 'Tran';
console.log(lastName)
// Still works, but should be avoidedBasic operators
Transform, combine, ... work with values
Math
const now = 2025;
const ageUser = now - 1994;
// 31
console.log(ageUser * 2, ageUser / 10);
console.log(2 ** 3);
// 2^3 = 8+ can be used to concat variables:
let x = "Volvo" + 16 + 4;
// Volvo164
x = 16 + 4 + "Volvo";
// 20VolvoAssignment
let x = 10 + 5;
x += 10; // x = x + 10 = 25
x *= 2; // x = x*2 = 50
x++; // x = x + 1
x--; // x = x - 1Comparision
Produce boolean
const birthYear = 2006;
const isAdults = 2025 - birthYear >= 18 // true
console.log(2025 - birthYear > 2025 - 2020) // true
// Operators: >,<,>=,<=Equality
Precedence
Check MDN page
Strings & Template Literals
const name = 'Loc'
const university = 'UMP'
console.log("I'm" + " " + name + ", who study at" + " " + university);
console.log(`I'm ${name}, who study at ${university}`);
// Same
console.log('String with \n\
multiple \n\
lines')
console.log(`String with
multiple
lines`)
// Sames, you should favor Template Literalsif - else Statements
let century;
const birthYear = 2001;
if (birthYear <=2000) {
century = 20;
} else {
century = 21;
}
console.log(century);Type Conversion & Coercion
// type conversion - manual conversion
const inputYear = '2000';
console.log(inputYear + 18); // 200018 - Doesn't work as expected
console.log(Number(inputYear) + 18); // 2018
console.log(Number('Loc')); // NaN - not a number
console.log(typeof NaN); // still type: number
console.log(String(23))// type coercion- automatic conversion
console.log("I'm " + 10 + "years old");
console.log("I'm " + '10' + "years old");
// The same
console.log('23' - '10' + 5); // 18
console.log('23' + '10' + 5); // 23105
console.log('10' / '2'); // 5
let n = '1' + 1;
n = n - 1; // 10
console.log(2+3+4+'5'); // 95Truthy & Falsy values
5 falsy values: 0, '', undefined, null, NaN -> convert to false when convert to boolean
console.log(Boolean(0), Boolean(undefined));
// false
console.log(Boolean('loc'), Boolean({}));
// true// Prefer type coercion
const money = 0;
if (money) {
console.log('Don\'t spend it all')
} else {
console.log('You should get a job!')
}let height;
if (height) {
console.log('Height is defined')
} else {
console.log('You should define height')
}
// Going to bug when you set height = 0;Miscs
'use strict'