JS/JavaScript

[JS] 04. Operator

밍글링글링 2017. 8. 28.
728x90

04_Operator.html

<script>
    //Concatenation
    var foo = "hello";
    var bar = "world";
    console.log(foo+" "+bar); //"hello world"
    
    //Multiplication and division
    2*3;
    2/3;
    
    //Incrementing and decrementing
    //The pre-increment operator increments the operand brfore any further processing.
    var i = 1;
    console.log(++i); //2 - because i was incremented before evaluation
    console.log(i); //2
    
    //The post-increment operator increments the operand after processing it.
    var i = 1;
    console.log(i++); //1 - because i was evaluated to 1 and_then_incremented
    console.log(i); //2 - incremented after using it
    
    // [Operations on Numbers & Strings]
    // Addition vs.Concatenation
    var foo = 1;
    var bar = "2";
    console.log(foo+bar); //12
    
    //Coercing a string to act as a number.
    var foo = 1;
    var bar = "2";
    console.log(foo+Number(bar)); //3
    
    //Forcing a string to act as a number (using the unary plus operator).
    console.log(foo+ +bar); //3
    
    //[Logical AND and OR Operators]
    var foo = 1;
    var bar = 0;
    var baz = 2;
    
    //returns 1, which is true
    console.log(foo || bar);
    
    //returns 1, which is true
    console.log(bar || foo);
    
    //returns 0, which is false
    console.log(foo && bar);
    
    //returns 2, which is true
    console.log(foo && baz);
    
    //returns 1, which is true
    console.log(baz && foo);
    
    //Do something with foo if foo is truthy.
    //foo && doSomething(foo);
    
    //Set bar to baz if baz is truthy:
    //otherwise, set it to the return value of createbar()
    var bar = baz || createBar();
    
    //[Comparison Operators]
    var foo = 1;
    var bar = 0;
    var baz = "1";
    var bim = 2;
    
    //equivalent
    console.log(foo==bar);//false
    console.log(foo!=bar);//true
    console.log(foo==baz);//true; but note that the types are different
    
    //idenfical
    console.log(foo===baz);//false
    console.log(foo!==baz);//true
    console.log(foo===parseInt(baz));//true
    
    console.log(foo>bim);//false
    console.log(bim>baz);//true
    console.log(foo<=baz);//true
</script>
 

728x90

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

[JS] 06. Loop  (0) 2017.08.28
[JS] 05. Conditional_Code  (0) 2017.08.28
[JS] 03. Type  (0) 2017.08.28
[JS] 02. SyntaxBasic  (0) 2017.08.28
[JS] 01. Running code  (0) 2017.08.28

댓글