본문 바로가기
LEARN/JAVASCRIPT

[자바스크립트] 기초부터 실전까지 올인원 실습 및 정리 01

by 아이엠제니 2022. 4. 18.

 

 

 

 

변수

let: 변수를 선언하는 것

변수의 이름은 변수의 id

변수의 이름은 겹치면 안 됨

 

let color = "pink"
color = "black"

console.log(color)

let을 안쓰면, 값을 바꿀 수 있음

 

 

const password = "pink"

console.log(password)

const 변수에 있는 값을 바꾸고 싶지 않을 때 사용.

값을 한번 할당하면 재할당이 안 됨.

 

var 사용 ㄴㄴ

 

 


자료형과 연산자

string: ""(큰따옴표), ''(작은따옴표) 안에 들어간 데이터. 문자로 취급. 문자열

숫자: 정수, 실수

boolean: true false

 

// 숫자
let password = 1
password = password + 2

console.log(password) // 3

 

// 논리 연산자

let password = 1
password = password > 2

console.log(password) // false

|| or

&& and

 

 


변수와 자료형 문제

문제 1. 와 b의 값을 바꾸시오

let a = 1;
let b = 2;
console.log(a,b);

let temp = a;
a = b;
b = temp;

console.log(a,b);

 

문제 2. 다음 연산자들의 결과값을 예측한 후 console.log()를 통해 확인해 보시오.

let a = 20 + 30; // 50
let b = "a" + "b"; // ab
let c = "Hello" + " " + 2021; // Hello 2021
let d = 1 + 2 * 3; // 7
let e = (1 + 3) ** 2; // 16 제곱
let f = 1 / 0; // Infinity
let g = 6 % 2; // 0
let h = 7.5 % 2; // 1.5
let i = 5 == 5 // true
let j = 5 === 5 // true
// let k = 5 == “5” // error
// let l = 5 === “5” // error
let m = 5 != 5.0 // false
let n = 5 !== 5.0 // false
// let o = “true” === true // error
let p = 5 <= 5.0 // true
let q = 5 >= 5 // true
let r = true || true // true
let s = true || false // true
let t = true && true // true
let u = true && false // false
let v = !true // false
let w = !false // true

 

 


보너스 트랙: 호이스팅, let과 var의 차이

함수만 지역변수

나머지는 전역변수


배열

// let fruit = "banana"
// let fruit2 = "apple"
// let fruit3 = "grape"
// let fruit4 = "mango"

let fruit = ["banana", "apple", "grape", "mango"]
console.log("배열 : " + fruit)
console.log("바나나 출력 : " + fruit[0])

fruit[0] = "cherry"

console.log("0번쨰 값 바꿈 : " + fruit)

fruit[2] = "tomato"

console.log("2번째 값 바꿈 : " + fruit)

// pop() : 마지막에 있는 아이템을 뺌
fruit.pop()
console.log("pop() : " + fruit)

// push() : 아이템을 배열 마지막에 추가함
fruit.push("pineapple")
console.log("push() : " + fruit)

// includes() : 해당 아이템을 배열이 포함하고 있는지 알려줌
console.log("includes() : " + fruit.includes("apple")) // true
console.log("includes() : " + fruit.includes("pear")) // false

// indexOf() : 아이템의 인덱스 번호 알려줌
console.log("indexOf() : " + fruit.indexOf("apple"))

// slice() & splice()
// slice() : 하나 또는 2개. 하나의 경우 시작점 , 2개의 경우 시작점과 끝점
// slice : 배열 아이템을 잘라내는 역할 (끝점 미포함)
// slice : 기존의 배열을 건들지 않음. 새로운 배열을 만든다
console.log("slice(시작점) : " + fruit.slice(2)) // 잘린 값이 반환됨 ["tomato","pineapple"]
console.log("slice(시작점,끝점) : " + fruit.slice(1,3)) // 3은 포함하지 않는다 ["apple","tomato"]
let extrafruit = fruit.slice(1,3)
console.log("new : " + extrafruit)
console.log("original : " + fruit)

// splice(시작점, 개수) : 시작점부터 몇 개의 아이템을 제거할지
// splice : 기존의 배열이 잘림
console.log("splice(시작점,개수) : " + fruit.splice(2,1))
console.log("splice() 이후 : " + fruit) // tomato 빠짐

// 수정본

animals[animals.indexOf("Red deer")] = "deer"
console.log(animals)
console.log(animals[77])


animals.splice(animals.indexOf("Spider"),3)
console.log(animals)

animals.splice(animals.indexOf("Tiger"))
console.log(animals)

let newAnimals = animals.slice(animals.indexOf("Baboon"),animals.indexOf("Bison")+1)
console.log(newAnimals)

 

 


객체

let patient = {
  name : "jimin",
  age : 17,
  disease : "cold"
}

console.log(patient)
console.log(patient.name)
console.log(patient.age)
console.log(patient["age"])

patient.name = "jk"
patient.age = 25
console.log(patient)

let patientList = [
  {name:"jimin",age:13},
  {name:"jk",age:25},
  {name:"jhope",age:40}
]
console.log(patientList)
console.log("첫번째 환자는:", patientList[0])
console.log("첫번째 환자의 나이는?:", patientList[0]["age"])

console.log("두번째 환자는:", patientList[1].name)

 

 

 

인프런 코딩알려주는누나 [기초부터 실전까지 올인원] 수강중

 

 

300x250