Javascript
JavaScript fundamentals, TypeScript, Frameworks
Index
Section | Link | Key Points |
---|---|---|
Introduction | Introduction | TypeScript Frameworks |
Variables | Variables | const let var |
Numbers and Operators | Numbers and Operators | strToNum parseInt parseFloat |
Strings | Strings | .toLowerCase() ${name} |
Arrays⭐ | Arrays | map() filter() reduce() |
Building Blocks | Building Blocks | conditions loops switch ?: |
Objects | Objects | |
Asynchronous JavaScript | Asynchronous JavaScript | |
TypeScript | TypeScript |
Introduction
JavaScript
- Dynamic client-side scripting language
- Universal & Easy to Start
- Built into all browsers
- No installation needed
- Huge ecosystem (npm)
TypeScript
- JavaScript with static typing
- Adds type safety and better tooling
- Considered best practice for large applications
Applications
Front-end
- React - Components with unified HTML/CSS/TS files
- Vue - Similar component structure to React
- Angular - Separates HTML/CSS/TS files
Back-end & Full-stack
- NodeJs
- NestJs, Hono
- Express
Variables
const
let
var
const
(recommended default)- Block-scoped, immutable reference
- Can modify properties of objects but not reassign
let
- Block-scoped, mutable
- Use when variable needs reassignment
var
(avoid)- Function-scoped, hoisted
- Legacy keyword, not recommended
Use const
- Use
const
by default - Use
let
only when reassignment is needed - Avoid
var
const greeting = "Hello World";
console.log(greeting);
// var example - function scope
function example() {
var x = 1;
if (true) {
var x = 2; // same x, if someone use the same name x in another function, it will be the same x
}
console.log(x); // outputs 2
}
// let example - block scope
function example2() {
let x = 1;
if (true) {
let x = 2; // new x in different scope, because let creates a new variable in the block scope
}
console.log(x); // outputs 1
}
// const example - immutable reference
const user = {name: "张三"};
user.name = "李四"; // can modify properties
user = {}; // error! cannot reassign so make sure the const is safe
Numbers and Operators
Definition
- All numbers in JavaScript are 64-bit floating-point (IEEE 754)
- Special values:
Infinity
,-Infinity
,NaN
BigInt
for integers larger than 2^53- Main operators:
- Arithmetic:
+
,-
,*
,/
,%
,**
(exponent) - Increment/Decrement:
++
,--
- Assignment:
=
,+=
,-=
,*=
,/=
,%=
- Comparison:
==
,===
,!=
,!==
,>
,<
,>=
,<=
- Logical:
&&
,||
,!
,??
(nullish coalescing)
- Arithmetic: