전체 글109 Java Script - 함수 function 함수는 같은 코드를 많이 작성할 때 사용할 수 있다. 함수를 작성하면 이후에 함수만 호출하면 된다. function employee(salary){ let rank = 0.4; let incentive = salary * rank; return incentive; } let incentive = employee(400); console.log(incentive); employee라는 함수를 선언하고 그 안에 코드를 작성한다. 같은 작업을 많이 할 경우, 함수만 불러와 변수만 바꾸면 쉽게 값을 얻을 수 있다. return 으로 반환할 값을 정할 수 있지만 없는 경우도 있다. 2022. 12. 5. Java Script - 반복문 while, for for이나 while문을 이용하면 반복되는 코드를 간편하게 작성할 수 있다. for(let i = 0; i < 5; i++){ console.log(i); } 제일 기본적인 for문의 문법이다. 변수 i를 선언하고 몇 번 반복할 건지 설정한다. 그리고 i를 1씩 증가시킨다. 그 후 i를 출력한다. 결과는 0 1 2 3 4 가 된다. for - in, for - of 반복문은 배열이나 오브젝트에서 사용할 수 있다. let number = [51, 7, 24, 6]; let len = number.length; for(let i in number){ console.log(number[i]); } for(let i of number){ console.log(i); } 첫번째 for문은 i가 인덱스 번호로 작용.. 2022. 12. 5. Java Script - 조건문 if else, switch 조건문은 조건을 설정해 적합하면 원하는 코드를 실행할 수 있다. let floor = 15; let floorname = ""; if (0 < floor && floor < 4) { console.log("low"); } else if (floor < 0) { console.log("Basement"); } else { console.log("high"); } switch (floor) { case 1: floorname = "Door"; break; case 15: floorname = "rooftop"; break; default: floorname = ""; break; } console.log(floorname); if의 조건에 맞으면 if {} 안의 코드를 실행하고 else 문들은 모두 건너 뛰.. 2022. 12. 5. Java Script - 연산자 Operator 연산자는 이미 알고 있는 연산자와 같다. let x = a + b + c; let y = a - b; let z = a * c; let i = a / c; let j = a ** b; let k = a % c; = - x 에 a, b, c를 더한 값을 할당 + - a, b, c 를 더한다 - - a에서 b를 뺀다 * - a와 c를 곱한다 / - a에서 c를 나눈다 ** - 거듭제곱 문자로 a^b (ex. 7**2 = 7^2 = 49) % - 나머지 구하기 (ex. 7%5 = 2) a += b; a -= b; a *= b; a /= c; a **= b; a %= c; += - a = a + b -= - a = a - b 이처럼 a에 연산한 값을 재할당 하는 연산자이다. let txt1 = "test"; l.. 2022. 12. 5. 이전 1 ··· 20 21 22 23 24 25 26 ··· 28 다음