演算、繰り返し文[JavaScript(ES 5+)]

32017 ワード

1. String concatenation
console.log('my' + 'cat');
console.log('1' + 2);//12
console.log(`string literals: 1 + 2 = ${1 + 2}`);
2.Numeric operators
console.log(1 + 1); //add
console.log(1 - 1); //substract
console.log(1 * 1); //divide
console.log(1 / 1); //multiply
console.log(5 % 2); //remainder
console.log(2 ** 3); //exponentiation
3. Increment and decrement operators
let counter = 2;
const preIncrement = ++counter;
//3
const postIncrement = counter++;
console.log(preIncrement, counter); // 3 4
const preDecrement = --counter;
//업데이트하고 할당
const postDecrement = counter--;
//할당하고 업데이트
4. Assignment operators
let x = 3;
let y = 6;
x += y; //x=x+y;
x -= y;
x *= y;
x /= y;
5. Comparison operators
console.log(10 < 6); //less than
console.log(10 <= 6); // less than or equal
console.log(10 > 6); // greater than
console.log(10 >= 6); //greater than or equal
6. Logical operators:
|| (or), && (and), ! (not)
const value1 = false;
const value2 = 4 < 2;

function check() {
    for (let i = 0; i < 10; i++) {
        //wasting time
        console.log('😱');
    }
    return true;
}
👉 || (or), finds the first truthy value
true値が見つかったら停止!
value 1がtrueの場合、時間出力xを無駄にする
console.log(`or: ${value1 || value2 || check()`});
Tip)
シンプルバリューの子供を一番前に、関数のように複雑な子供を後ろに!
👉 && (and), finds the first falsy value
value 1がfalseの場合は後ろのxをオフにします
console.log(`and: ${value1 && value2 && check()}`);
null値であるかどうかを確認するために使用します.
often used to compress long if-statement
nullableObject && nullableObject.something
以下
if(nullableObject != null){
    nullableObject.something;
}
👉 ! (not)
console.log(!value1);
7. Equality
const stringFive = '5';
const numberFive = 5;
== loose equality, with type conversion
console.log(stringFive == numberFive); //true
console.log(stringFive != numberFive); //false
=== strict equality, no type conversion
console.log(stringFive === numberFive); //false
console.log(stringFive !== numberFive); //true
object equality by reference
const ellie1 = {name: 'ellie'};
const ellie2 = {name: 'ellie'};
const ellie3 = ellie1;
console.log(ellie1 == ellie2); //false
console.log(ellie1 === ellie2); //false
console.log(ellie1 === ellie3); //true
equality - puzzler
console.log(0 == false);
//true
console.log(0 === false);
//false 0은 boolean type 아니다.
console.log('' == false);
//true
console.log('' === false);
//false
console.log(null == undefined);
//true
console.log(null === undefined);
//false
8. Conditional operators: if
if, else if, else
const name = 'annie';
if (name === 'annie') {
    console.log('welcome annie');
} else if (name === 'coder') {
    console.log('You are amazing coder');
} else {
    console.log('unknown');
}
9. Ternary operator: ?condition ? value1 : value2; console.log(name === 'annie' ? 'yes' : 'no');10. Switch statement
use for multiple if checks
use for enum-like value checks
use for multiple type checks in TS
const browser = 'IE';
switch (browser) {
    case 'IE':
        console.log('go away');
        break;
    case 'Chrome':
    case 'Firefox':
        console.log('love ya');
        break;
    default:
        console.log('same all');
        break;
}
11. Loops
while loop, while the condition is truthy,
body code is executed
まず条件文が正しいかどうか見てみましょう.
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}`);
}
inline variable declaration
for (let i = 3; i > 0; i = i - 2) {
    console.log(`inline 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}`);
    }
}
Bigo=O(n**2)はCPUに不利
  • break:loop完全終了
  • continue:ここまで、次の
  • に進みます.
    Q1. iterate from 0 to 10 and print only even numbers (use continue)
    for(let i=0;i<11;i++){
        if(i%2 !== 0){
            continue;
        }
        console.log(i);
    }
    
    for (let i = 0; i < 11; i++) {
        if (i % 2 === 0) {
            console.log(i);
        }
    }
    Q2. iterate from 0 to 10 and print numbers until reaching 8 (use break)
    for(let i = 0; i< 11; i++){
        if(i > 8){
            break;
        }
        console.log(i);
    }