day2 - Arrays

  • 데이터를 나열하기 위한 방법 중 하나.
  • 항상 [ ] 안에 콤마(,)로 데이터들을 나열한다.
  • 변수도 쓰일 수 있고, boolean, text, 숫자 등 데이터 정렬이 가능
//ex)
const daysOfWeek = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

만약, 위의 변수에서 5번째 element 값을 알려주세요. 라고 한다면 어떻게 출력해야 할까?

//ex) 
console.log(daysOfWeek[4]) //라고 해야 5번째 값을 출력할 수 있다.
  • 왜?? 컴퓨터는 숫자를 0부터 세기 때문에

위의 상태에서 daysOfWeek이란 변수에 하나의 값을 더 넣고 싶다면 다음과 같이한다.

//ex)
daysOfWeek.push(“holiday”) 
  • .push는 추가하는 기능.

day2 - Object

  • 객체는 의미있는 배열을 만든다는 개념임
  • 그냥 배열은 값들 만있지만 객체는 키와 값이 있어 그 값의 의미를 알 수 있다는 점
  • object는 property를 가진 데이터를 저장해주며, { } 를 사용한다.
//ex)
const player = {
name : tomato,
color : red,
food : true,
};

console.log(player);

property를 불러오는 방법은 2가지가 있다.

1. console.log(player.name); => tomato
2. console.log(player["name"]); => tomato

‼️ 또한 property를 바꾸는 것은 가능하지만 선언된 object를 바꾸는 것은 불가능하다.

//ex) 
const player = {
name : tomato,
color : red,
food : true,
};
console.log(player);
player.color = "blue";
console.log(player.color);
--> blue

그리고 property를 추가 할 수도 있다.

player.koreanName = "토마토";
--> {name: "tomato", color: "blue", food: true, koreaName: "토마토"}

day3 - function

  • 함수 반복해서 사용할수있는 조각코드
  • 코드를 캡슐화해서 재사용 가능하게 해줌
  • 함수에게 고정된 코드를 지정하고
  • 함수 밖에서 데이터를 받아 함수에게 전달해줘야함
  • 함수는 결과를 받아 줘야함 => return
  • return 후에 쓴 코드는 작동안함
  • return 하자마자 함수는 종료함
  • 함수를 실행하는법 함수이름()
const player = {
	name:"nico",
  	sayHello:funtion(personName){
	console.log("hello"+personName)		
	};
};

player.sayHello("ssonni"); //payer객체의 함수에게 personName인수에게 ssonni라는 인자를 넘김

day3 - conditionals

  • isNaN 은 무언가가 NaN인지 판별하는 방법
ex)
const age= parseInt(prompt (“How old are you?));
console.log(isNaN(age));
  • 숫자입력하면 false

  • 글자입력하면 true

  • 조건문

if(condition){
실행코드=true ---실행 
실행코드=false ----다음 else 값 실행
} else{
	...
}
  • condition은 boolean으로 판별가능해야 한다(true , false)

const age = parseInt(prompt("How old are you?"));

if(isNaN(age)|| age<0){
    console.log("Please write number");
} else if( age < 18){
    console.log("you are too young");
} else if(age>=18 && age <=50) {
    console.log("You can drink");
}
else if(age>=50 && age <=80) {
    console.log("You should exercise");
} else if(age>80){
    console.log("you can do whatever u want");
}
  • parseInt() : string -> number 바꾸는 함수
  • 연산자(== 동등연산자 , === 일치연산자 )
    = value 할당
    == 값만 같으면 true
    === 값과 값의 타입 모두 같으면 true
    !== 아닌지 확인

null = 없는 값 메모리 O

undefinded = 값 없음 메모리 x

profile
냠소현 개발일지

0개의 댓글