본문 바로가기

WEB/Javascript

[JS] 기본문법 -함수/ 조건문/ 반복문

1. 함수

  1. 화살표 함수위와 같은 함수를 간략하게 나타낼 수 있다.
  2. function funct1(a,b,c){ const result = a*b+c; return result } const result1=funct1(5,4,7) console.log(result1)//27 console.log(funct1(6,4,3) //27
const funct1 = (a,b,c) => a*b+c;
result2=funct1(13,2,1)
console.log(result2)//27
  1. (...param) 함수함수값과 개수가 미정일 때 사용하기 좋다.
  2. const funct3=(...a)=>{ let b=0 for (const i of a){ b += i } return b }; c=funct3(10,50,60,40); console.log(c); //160

2. 조건문

  1. if 조건문
    const a = 3500; if (a<5100){ console.log("we can pay!") } else if (a>=5100){ console.log("we need more money") } else { console.log("there is no items") } //we can pay!
  2. switch 조건문
    Switch 는 ===비교로 조건문을 실행한다. 데이터 타입까지 일치해야한다.
    const a = 35; switch (a){ case 10000: console.log("hi"); break; case 15000: console.log("hello"); break; default : console.log("bye"); break; } //bye

3. 반복문

  1. 반복문for (조건){실행}
  2. for (let b=3; b<5; b++){ console.log(b); }//3,4차례로 출력

b++은 증감연산자로서 b=3 에서부터 1씩 증가시킨다,
그러니까 위와 같은 코드는 b=3이 5보다 작으니까 출력하고 그후 1증가 시켜서 4가되서 다시 5보다 작으니까 출력하고 다시 1증가 시켜서 5가되서 5보다 작지 않으니까 반복문이 종료된다.

```
const a=[4,6,8]
let b= 0
for (const i of a){
 b += i
}
console.log(b) //18
```
  1. While 반복문
    let a= 0;
    while (a<5){
     console.log(a);
     a+=1;
    }//0,1,2,3,4

'WEB > Javascript' 카테고리의 다른 글

[JS] 기본문법 - 문자열  (0) 2022.05.13
[JS] 기본문법-숫자  (0) 2022.05.05
[JS] 기본문법-boolean  (0) 2022.05.04
[JS] 기본 문법 -변수 선언  (0) 2022.05.04
[JS] 자바스크립트의 개념과 기초구현  (0) 2022.05.04