본문 바로가기

Web Front-end/javaScript

and , or 연산자로 코드 짧게 쓰기( &&, || )

반응형

[1] or 연산자

//테스트용 객체
const person = {
            name: "한국인",
            age: 20,
            job: "student"
        }
        or연산자는
        첫번째 값이 존재하면 첫째값을 return ,
        그렇지 않으면 두번째 값을 리턴

        기본값 설정할때 많이 사용
 
        console.log(person.name || '이름이 없습니다.')  //한국인
        console.log(person.address || '주소가 없습니다.') //주소가 없습니다.

 

 

[2] and 연산자

        && 연산자는
        두개의 값이 모두 참이면 마지막값을 return,
        둘중에 하나라도 false면 false를 리턴

        특수한 상황의 조건문에 많이 사용

 

        console.log(person.age < 20 && '운전불가능'); //false
        console.log(person.age > 19 && '운전가능'); //운전가능

 

 

 

 


Reference 

https://youtube.com/shorts/4X7WqELD-Ls?feature=share

반응형