Front-end π₯π€/JavaScript
π λλ¦Όμ½λ© by μ리 JavaScript μ 리 2. λ°μ΄ν° νμ
stable_sound
2022. 1. 21. 23:59
1. variable : λ³μ
: λ³κ²½λ μ μλ κ°
let : ES6μμ μΆκ°
let name = `ellie`;
console.log(name); // > ellie
name = `hello`;
console.log(name); // > hello
2. Block scope
let globalName = "global name";
//global λ³μλ μ΄ν리μΌμ΄μ
μμλΆν° λλ λκΉμ§ λ©λͺ¨λ¦¬μ μκΈ° λλ¬Έμ μ΅μνμΌλ‘ μ°λ κ²μ΄ μ’λ€.
{
let name = "ellie";
console.log(name);
name = "hello";
console.log(name);
}
console.log(globalName); // global λ³μμ΄κΈ° λλ¬Έμ μΆλ ₯κ°λ₯ > global name
console.log(name); // global λ³μκ° μλκΈ° λλ¬Έμ λ³μλ₯Ό μ μΈν λΈλ‘μ λ΄λΆμμλ§ μΆλ ₯ κ°λ₯ > error
Block scope μ΄μΌκΈ°λ₯Ό νλ©΄ λΉ μ§μ§ μλ κ²μ΄ var μ΄λ€.
μ΄ varλ νμ¬λ μ¬μ©νμ§ μλλ° μ΄μ κ° μλ€.
// varλ μ°μ§λ§μ!
console.log(age); // > undefined
age = 4;
console.log(age); // > 4
var age;
// μ¬μ§μ΄ varλ λΈλ‘ μ€μ½νλ μλ€.
{
age1 = 5;
var age1;
}
console.log(age1); // λΈλ‘ μμμ μ μΈν΄λ μ΄λμλ μ¬μ© κ°λ₯νλ€.
varμ κ²½μ° μ μΈμ μμ§ νμ§ μμλλ°λ μ½μμμ undefinedλ‘ μΆλ ₯μ΄ κ°λ₯νλ€.
λΆλͺ ν μμ§ μ μΈμ μνλλ°.. μ λ§ μ΄μν λ μμ΄λ€. γ γ γ γ
μ΄λ° νμμ λ°λ‘ νΈμ΄μ€ν (λμ΄μ¬λ¦¬λ€) μ΄λΌκ³ λΆλ₯Έλ€.
var hoisting : μ΄λμ μ μΈνλμ§μ μκ΄μμ΄ μ μΌ μλ‘ μ μΈμ λμ΄ μ¬λ¦¬λ κ²
κ·Έλ¦¬κ³ varλ λΈλ‘ λ²μκ° μκΈ° λλ¬Έμ μ μΈλ§ νλ©΄ μ΄λμλ μ¬μ©ν μ μλ€.
κ·Έλ κΈ° λλ¬Έμ varλ μ¬μ©νμ§λ§μ!
3. Constant
: νλ² ν λΉνλ©΄ λ³κ²½μ΄ λΆκ°λ₯ν κ°
const max = 5;
max = 11; // > error
/* const λ₯Ό μ¬μ©νλ μ΄μ
* 보μ : ν΄μ»€λ€μ΄ κ°μ κ³μ λ³κ²½ν μ μλλ° μ΄λ₯Ό λ°©μ§ν μ μλ€.
* μ€λ λ μμ μ± : νλ‘μΈμ€ μμμ λ€μν μ€λ λκ° λμμ μ€νλλλ° λμμ λ³μμ μ κ·Όν΄μ κ°μ λ³κ²½ν μ μλλ° μ΄κ² λΆκ°λ₯νλλ‘ ν΄μΌνλ€.
* μΈκ°μ μ€μ κ°μ
*/
4. Variable types
// 4. λ°μ΄ν° νμ
// primitive, single item: number, string, boolean, null, undefined, symbol
// object, box container
// function, first-class function
//number
const count = 17;
const size = 17.1;
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = `not a number` / 2;
console.log(`value: ${count}, type:${typeof count}`);
console.log(`value: ${size}, type:${typeof size}`);
console.log(`value: ${infinity}, type:${typeof infinity}`);
console.log(`value: ${negativeInfinity}, type:${typeof negativeInfinity}`);
console.log(`value: ${nAn}, type:${typeof nAn}`);
//string
const hello = "hello";
const greeting = hello + "name my is Lee";
console.log("value: " + hello + " type: " + typeof hello);
console.log(`value: ${hello}, type: ${typeof hello}`); //ν
νλ¦Ώ 리ν°λ΄μ΄ ν¨μ¬ νΈνλ€.
//boolean (true/false)
const t = true;
const test = 3 < 1; // false
console.log(`value: ${t}, type: ${typeof t}`);
console.log(`value: ${test}, type: ${typeof test}`);
//null (μλ€ ν
ν
λΉμ΄μλ€)
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);
//undefined (μ μΈμ λμ΄μμ§λ§ μ무κ²λ κ°μ΄ μ§μ λμ΄μμ§ μλ€)
let x = undefined;
console.log(`value: ${x}, type: ${typeof x}`);
//symbol
//κ°μ²΄μ λν κ³ μ μλ³μλ₯Ό μμ±
const symbol1 = Symbol("id");
const symbol2 = Symbol("id");
console.log(symbol1 === symbol2); // > false : λμΌν stringμ μμ±ν΄λ λ€λ₯Έ symbolλ‘ λ§λ€μ΄μ§
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
// object
const stable_sound = { name: "Lee", age: 24 };
// sound μ체λ const λ‘ μ μλμ΄μκΈ° λλ¬Έμ λ€λ₯Έ objectλ‘ ν λΉμ΄ λΆκ°λ₯.
// νμ§λ§ κ·Έ μμ μλ nameκ³Ό age λ³μλ λ€λ₯Έκ°μΌλ‘ ν λΉμ΄ κ°λ₯νλ€.
console.log(stable_sound);
stable_sound.age = 23;
console.log(stable_sound);
5. Dynamic typing (λμ νμ΄ν)
: λ³μ μ§μ μ, ν΄λΉ λ³μμ λ°μ΄ν° νμ μ λͺ μνμ§ μμλ μλμ μΌλ‘ νμ μ ν΄μνλ€.
let text = `hello`;
console.log(text.charAt(0)); // > h
console.log(`value: ${text}, type: ${typeof text}`); // > type: string
text = 1;
console.log(`value: ${text}, type: ${typeof text}`); // > type: number
text = `7` + 5;
console.log(`value: ${text}, type: ${typeof text}`); // > type: string
text = `8` / `2`;
console.log(`value: ${text}, type: ${typeof text}`); // > type: number
// console.log(text.charAt(0)); // > error : Uncaught TypeError: text.charAt is not a function
// jsμμλ λ°νμμ νμ
μ΄ μ ν΄μ§λλ° νμ
μ΄ μκΎΈλ§ λ³νκ² λλκΉ μλ¬κ° λ°μνλ€.
// κ·Έλμ λμ¨ κ²μ΄ typescriptμ΄λ€.