JavaScript
1. κ°λ°νκ²½ κ΅¬μΆ λ° μλ°μ€ν¬λ¦½νΈ κΈ°λ³Έ
β’
VSC(Visual Studio Code) μΈν
β’
Live Server μ€μΉ
β’
ES6 μΉ νμ€
β’
node.js β runtime
β¦
μλ°μ€ν¬λ¦½νΈ μ€νν μ μλ νκ²½μ λ§λ€μ΄μ€
β’
μμμ μλλ‘ ν μ€ μ© μ½μΌλ©΄μ μ€νλ¨
β’
alert
β¦
script νκ·Έ μμ javascript μ½λλ₯Ό μμ±νλ€.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
<!-- script νκ·Έ νμ©νμ¬ javascript μ€ν-->
<script>
alert('Hello~!!')
</script>
</head>
<body>
</body>
</html>
HTML
볡μ¬
2. μλ£νκ³Ό λ³μ, 쑰건문과 λ°λ³΅λ¬Έ
β’
μλ£(data): νλ‘κ·Έλ¨μ΄ μ²λ¦¬ν μ μλ κ²
β’
μλ£ν(data type): μλ£ ννμ λ°λΌ λλ λμ κ²
β¦
μ«μ(number)
β¦
λ¬Έμμ΄(string)
β¦
λΆλ¦¬μΈ(boolean)
β’
λ¬Έμμ΄ μλ£ν
β¦
λ¬Έμμ΄μ κ°μ²΄μ²λΌ μ°μ¬μ§
βͺ
βμβ, βλ
β, βνβ, βμΈβ, βμβ β βμλ
νμΈμβ
(κ°μ²΄λ λ¬Άμμ΄λ€ β λ³μλ€, ν¨μλ€)
β¦
μλ°μ€ν¬λ¦½νΈλ 2κ°μ§ λ°©λ²μΌλ‘ λ¬Έμμ΄ μμ±
βͺ
ν°λ°μ΄ν
βͺ
μμλ°μ΄ν
β’
λ¬Έμμ΄ μμ
β’
μ£Όμ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
<!-- script νκ·Έ νμ©νμ¬ javascript μ€ν-->
<script>
// alert('Hello~!!') // ν μ€ μ£Όμ
/* alert(111)
alert(222) */
// λΈλ μ£Όμ
</script>
</head>
<body>
</body>
</html>
HTML
볡μ¬
β’
λΉκ΅ μ°μ°μ
β¦
β==β μ β===β μ μ°¨μ΄μ
βͺ
β==β λ κ°λ§ μΌμΉν΄λ λμ§λ§ β===βλ νμ
κΉμ§ μΌμΉν΄μΌ ν¨
β’
!=, !== λ λ§μ°¬κ°μ§
β¦
βfalse == []β β true μΆλ ₯λ¨
βͺ
λ°°μ΄μ μμκ° μ무κ²λ μμΌλ©΄ λμμ νμ§ μμ
β’
λ
Όλ¦¬ μ°μ°μ
β¦
AND: &&, ~μ΄κ³ , and
X | Y | R |
true | true | true |
true | false | false |
false | false | false |
β¦
OR: ||, μ’λ³ λλ μ°λ³μ΄ true μ΄λ©΄ κ²°κ³Όλ true
X | Y | R |
true | true | true |
true | false | true |
false | false | false |
β’
let μ¬μ© μ λ°μν μ μλ μλ¬
β’
λμ
μ°μ° : λ³μ = κ°(리ν°λ΄) λλ λ³μ
// λμ
μ°μ° : λ³μ = κ°(리ν°λ΄) λλ λ³μ
let num7 = num6;
console.log('num7 => ', num7)
JavaScript
볡μ¬
β’
λ³΅ν© λμ
μ°μ°μ
num8 = 100
num8 = num8 + 10 // => num8 = 100 + 10 => num8 = 110
console.log('num8 = num8 + 10 =>', num8)
num8 += 100 // num8 = num8 + 100 => num8 = 110 + 100
console.log('num8 += 100 => ', num8)
JavaScript
볡μ¬
β’
λ¨ν μ°μ°μ : νλμ λ³μμ νλμ λΆνΈ
μ 리
// λ³μ μ μΈ : var, let, const
// λ³μ : λ°μ΄ν°μ μ°Έμ‘°κ°(λ©λͺ¨λ¦¬ μ£Όμ)μ μ μ₯ => λ΄κ° μ μ₯νκ³ μ νλ λ°μ΄ν°μ λ©λͺ¨λ¦¬ μ£Όμ μ μ₯
// λ€λ₯Έ κ°μ μ μ₯νλ κ²λ κ°λ₯
// νμ
: μ«μ(number), λ¬Έμμ΄(string), λ
Όλ¦¬(boolean), ν¨μ(function)
// κ°μ²΄(object), λ°°μ΄[](object)
// undefined => μ μΈλμ§ μμ λ³μ νΈμΆ, λ°μ΄ν° μ΄κΈ°ννμ§ μμμ λ
// μ«μ : μ¬μΉμ°μ°( + - * / % ), λμ
μ°μ°, 볡ν©λμ
μ°μ°μ
// 볡ν©λμ
μ°μ°μ( += -= *= /= %= ), λ¨ν (+ -),
// μ¦κ° μ°μ°μ ( ++ -- ) : μ μ, νμ, 1μ© μ¦κ°
// λ
Όλ¦¬ : boolean => true / false
// λΉκ΅μ°μ°μ ( == ===, !=, !==, >, <, >=, <= )
// λ
Όλ¦¬μ°μ°μ ( ! λ
Όλ¦¬λΆμ μ°μ°μ, &&, || )
JavaScript
볡μ¬
3. ν¨μμ κ°μ²΄, λ¬Έμ κ°μ²΄
β’
μ¬μ©μκ° λ©μμ§λ₯Ό μ
λ ₯νλ ν¨μ
let username = prompt('μ΄λ¦μ μ
λ ₯νμΈμ') // λκΈ°
console.log('μ¬μ©μκ° μ
λ ₯ν μ΄λ¦ => ', username)
let birth_year = prompt('νμ΄λ λ
λλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ')
console.log('birth_year', birth_year, typeof(birth_year))
let year_number = Number(birth_year)
console.log('year_number => ', year_number, typeof(year_number))
// λ¬Έμμ΄ => μ«μ νμ
λ³κ²½
// Number(λ¬Έμμ΄) => μ«μνμ
μ κ°μΌλ‘ λ³ν (NaN => Not a Number)
let age = 2024 - year_number
console.log('μ±μΈ νλ³ => ', age >= 19)
JavaScript
볡μ¬
β’
ν λ³ν
/ λ¬Έμμ΄λ‘ λ³ν : string
// μ«μ -> λ¬Έμμ΄, bool -> λ¬Έμμ΄
let num10 = 100;
// console.log('num10 => ', num10, typeof(num10))
let number_str1 = String(num10)
// console.log('number_str1', number_str1, typeof(number_str1))
let number_str2 = num10 + '' // μ«μ + λ¬Έμμ΄ => λ¬Έμμ΄ + λ¬Έμμ΄ => λ¬Έμμ΄
// console.log('number_str2', number_str2, typeof (number_str2))
let check = true
// console.log('check => ', check, typeof(check))
let chk_str1 = String(check)
// console.log('chk_str1 => ', chk_str1, typeof(chk_str1))
let chk_str2 = check + '' // bool + string => string + string => string
// console.log('chk_str2 => ', chk_str2, typeof (chk_str2))
// string -> bool, number -> bool
// Boolean(κ°) => λ
Όλ¦¬κ°
// 0, '', null, undefined, NaN, [] => false
// ! λ
Όλ¦¬λΆμ μ°μ°μ : ! λ
Όλ¦¬κ°
JavaScript
볡μ¬
β’
boolean μ λν νλ³ν
// string -> bool, number -> bool
// Boolean(κ°) => λ
Όλ¦¬κ°
// 0, '', null, undefined, NaN, [] => false
// ! λ
Όλ¦¬λΆμ μ°μ°μ : ! λ
Όλ¦¬κ°
console.log('!1 => ', !1) // true => false
console.log('!\'\'', !'')
console.log('!\'aaa\'', !'aaa')
console.log(Boolean(1)) // true
console.log(Boolean('abc')) // true
console.log(Boolean([])) // true
console.log(Boolean({})) // true
console.log(Boolean(0)) // false
console.log(Boolean(NaN)) // false
console.log(Boolean('')) // false
let num111 // undefined
console.log(Boolean(num111)) // false
console.log(Boolean(null)) // null
JavaScript
볡μ¬
β’
쑰건문
β¦
νλ‘κ·Έλ¨ νλ¦ μ μ΄
β¦
λΆκΈ°, λ°λ³΅
β¦
보λ β κ²μν (νμ 리μ€νΈ, μν 리μ€νΈ)
β¦
μ€μ , μ€ν κ΅¬λΆ λ‘μ§
// νμ¬ μκ°μμ μ€μ κ³Ό μ€νλ₯Ό ꡬλΆνλ μ½λ μμ±
const date = new Date()
// μκ° λΉκ΅: μκ° >= 12 μ€ν
if (date.getHours() >= 12) {
console.log('μ€ν')
}
if (date.getHours() < 12){
console.log('μ€μ ')
}
JavaScript
볡μ¬
β’
νμ νν
// A : score >= 90
// B : score >= 80 && score < 90
// C : score >= 70 && score < 80
// F : score < 70
let score = prompt('νμ μ μ
λ ₯νμΈμ')
if (score >= 90) {
console.log('A')
} else if (score >= 80) {
console.log('B')
} else if (score >= 70) {
console.log('C')
} else {
console.log('F')
}
JavaScript
볡μ¬
β’
λμ μμ±
const rand_num = Math.random() // 0λ³΄λ€ ν¬κ±°λ κ°μ μ€μλ‘ νν
console.log('rand_num', rand_num)
const real_num = Math.floor(rand_num * 100) + 1 // Math.floor(3.1258455)
console.log('real_num => ', real_num)
if (real_num >= 20) {
console.log('νμ΄ ~')
} else if (real_num <= 80) {
console.log('λ‘μ° ~')
}
JavaScript
볡μ¬
β’
μ¬μ©μλ‘λΆν° μ«μ λ°μ΄ν° μ
λ ₯λ°μμ νμ μ§μ νλ¨νλ νλ‘κ·Έλ¨
let temp_number = prompt('μ«μλ₯Ό μ
λ ₯νμΈμ')
if (isNaN(temp_number)) {
alert('μ¬λ°λ₯Έ μ«μ νμμ΄ μλλλ€.')
} else {
if (temp_number % 2 == 0) {
console.log('μ§μ')
} else if (temp_number != 0) {
console.log('νμ')
}
}
JavaScript
볡μ¬
κΈ°ν
β’
μλ¬
β¦
λ¬Έλ² μλ¬















