rhanziy

javascript. 연산자, 반복문 본문

Html_css_js

javascript. 연산자, 반복문

rhanziy 2022. 1. 17. 00:19
// string concatenation
console.log('my' + 'cat');
console.log('1'+2);
console.log(`string literals: 1+2 = ${1+2}`);
console.log (`rhanyi's \n\tbook`); // \n 줄바꿈 \t 탭


// numeric operators
console.log(1+1); //add
console.log(1-1); // subtract
console.log(1/1); // divide
console.log(1*1); // multiply
console.log(5%2); // remainder
console.log(2**3); //exponentiation

// Increment and decrement operators
let counter = 2;

const preIncrement = ++counter;
// 1. counter = counter + 1 ;   선행
// 2. preIncrement = counter;
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`);

const postIncrement = counter ++;
// 1. postIncrement = counter;
// 2. counter = counter+1;  후행
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`);

// Assignment oprators
let x = 3;
let y = 6;
x += y; // x = x+y; 누적
//x -= y; x*=y; x/=y;

// Comparison operators
console.log(10<6);   // less than
console.log(1<=1);  // less than or equal
console.log(1>1);   // greater than
console.log(1>=1);  // greater than or equal

// Logical operators :  || (or), &&(and), !(not)
const value1 = false;
const value2 = 4 < 2;
console.log(`or: ${ value1 || value2 || check() }`);  // value1이 true면 끝.
console.log(`and: ${ value1 && value2 && check() }`);  // value1이 false면 끝.

function check() {
    for (let i = 0; i <10; i++){
        console.log('ㅇ_ㅇ');
    }
    return true;
}

console.log(!value1);  // true

// Equality
const stringFive = '5';
const numberFive = '5';
// == loose equality, with type conversion
console.log( stringFive == numberFive ); 
console.log( stringFive != numberFive );
// === strict equality, no type conversion
console.log( stringFive === numberFive ); // 자료형까지 비교
console.log( stringFive !== numberFive );
// object equality by reference
const rhanyi1 = { name:'rhanyi' };
const rhanyi2 = { name:'rhanyi' };
const rhanyi3 = rhanyi1;
console.log( rhanyi1 == rhanyi2 );  // false
console.log( rhanyi1 === rhanyi2 );  // false
console.log( rhanyi1 == rhanyi3 );  // true
// equality - puzzler
console.log( 0 == false ); // true
console.log ( 0 === false );  // boolean타입 x -> false 
console.log( '' == false); // true
console.log( '' === false ); // boolean타입 x -> false 
console.log( null == undefined ); // true
console.log( null === undefined ); // false

// Ternary operator
console.log( name === 'rhanyi' ? 'yes' : 'no');

// Switch statement
const browser = 'Safari';
switch (browser) {
    case 'IE':
        console.log('go away!');
        break;
    case 'Chrome':
    case 'Firefox':
        console.log('hi there~');
        break;
    default:
        console.log('same all!');
        break;
}

// Loops
let i = 3;
while( i > 0 ) {
    console.log(`while: ${i}`);
    i--;
}
// do while loop, body code is executed first, then check the condition
do {
    console.log(`do while : ${i}`);
    i--;
} while ( i > 0 );
// for loop, for(begin; condition; step)
for ( i = 3; i > 0; i -- ){
    console.log(`for: ${i}`);
}
for ( let i = 3; i > 0; i = i - 2 ){
    console.log(`iline variable for: ${i}`);
}
// nested loops
for ( let i = 0; i < 10; i ++ ){
    for( let j = 0; j < 10; j++ ){
        console.log(`i: ${i}, j: ${j}`);
    }
}
// test continue, break
for ( i = 0; i < 11; i++ ) {
    if( i % 2 === 0 ) {
        continue;  // skip개념
    }
    console.log(i);
}
for ( i=0; i < 11; i++ ){
    if( i > 8 ) {
        break;
    }
    console.log(i);
}
Comments