ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JS 알아보기 (연산자)
    JavaScript/JavaScript 2021. 5. 28. 05:09

     

    산술 연산자 (arithmetic operator)

     

    // 산술 연산자 (arithmetic operator)
    
    // 3
    console.log(1 + 2)
    // -2
    console.log(5 - 7)
    // 12
    console.log(3 * 4)
    // 5
    console.log(10 / 2)
    // 2
    console.log(7 % 5)

     

    할당 연산자 (assignment operator)

     

    // 할당 연산자 (assignment operator)
    
    let a = 2
    
    // a = a + 1
    a += 1
    
    // a -= 1
    // a *= 1
    // a /= 1
    // a %= 1
    
    // 3
    console.log(a)

     

    비교 연산자 (comparison operator)

     

    // 비교 연산자 (comparison operator)
    
    const a = 1
    const b = 3
    
    // a 가 b 보다 크면 true, 아니라면 false
    console.log(a > b)
    // a 가 b 보다 크거나 같으면 true, 아니라면 false
    console.log(a >= b)
    
    // === 일치 연산자, 데이터 타입과 값이 동일하면 true, 아니라면 false 
    console.log(a === b)
    
    function isEqual(x, y) {
      return x === y
    }
    
    // true
    console.log(isEqual(1, 1))
    // false
    console.log(isEqual(2, '2'))
    
    // 불일치 연산자, 데이터 타입과 값이 동일하면 false, 아니라면 true
    console.log(a !== b)

     

    논리 연산자 (logical operator)

     

    // 논리 연산자 (logical operator)
    
    const a = 1 === 1
    const b = 'AB' === 'AB'
    const c = false
    
    // true
    console.log(a)
    // true
    console.log(b)
    // false
    console.log(c)
    
    // AND 연산자, a 와 b 와 c가 모두 true라면 true, 1개라도 false라면 false
    // &&:  false
    console.log('&&: ', a && b && c)
    
    // OR 연산자, a 또는 b 또는 c가 1개라도 true라면 true, 아니라면 false
    // ||:  true
    console.log('||: ', a || b || c) 
    
    // NOT 연산자, a 값이 true라면 false , false라면 true
    // !: false
    console.log('!: ', !a) 

     

    삼항 연산자 (ternary operator)

     

    // 삼항 연산자 (ternary operator)
    
    // true
    const a = 1 < 2
    
    if (a) {
      console.log('참')
    } else {
      console.log('거짓')
    }
    
    // 위와 동일한 코드
    console.log(a ? '참' : '거짓')

    'JavaScript > JavaScript' 카테고리의 다른 글

    JS 알아보기 (변수 유효범위와 형 변환)  (0) 2021.06.02
    JS 알아보기 (조건문과 반복문)  (0) 2021.06.02
    JS 알아보기  (0) 2021.05.28
    JS 메소드 체이닝  (0) 2021.05.05
    JS 맛보기 (DOM API)  (0) 2021.05.03

    댓글

Designed by Tistory.