JSを使ったフロー制御


はい、JS でコードを書く方法を学んでいるときに最も重要なトピックの 1 つです 🤔

まず、コードがどの文を実行する必要があるかを決定する必要がある場合、それが何を意味するのか、正確にはフロー制御とは何かを理解する必要があります.それは FLOW CONTROL 😇 で行われます. JS を使用してフローを制御するにはさまざまな方法があります.いくつかの例を見てみましょう.

if - else 句



これは最も典型的なもので、コードの実行に関する多くの制御を提供し、どのコードを実行するかを決定できます.

if (condition) {
 // run here is condition is true
} else {
 // run here is condition is false
}


これはあなたが使用しなければならない基本的な構文です😁、それは本当に簡単です.評価される条件があり、それに応じてどちらか一方を実行します.ああ、神様!!はい、それは私たちに多くの力を与えてくれます.もう 1 つの例を試してみましょう.

const myMum = "Maria";
if (typeof myMum === 'string') {
 console.log("My mum contains a string");
} else {
 console.log("My mum is not a string");
}


先ほど見たように、このコードは、この conststring であるかどうかをチェックし、条件に従って次の文を実行します.明らかに、この条件は true であり、My Mum contains a stringconsole が表示されます😇.

さらに、 logic operators 、 AND && 、および OR || を使用して複数の条件を評価できます.
  • AND && : 条件全体の true により、すべての条件が真でなければならないことを意味します.

  • // It returns true due to both sides are true.
    true===true
    


  • または || : 最終的な true を取得するには、条件の少なくとも 1 つが真である必要があることを意味します.

  • // it returns true due to at least one side is true
    true || false
    


    同じ if 句を使用して複数の条件をチェックするためのより強力なツールを提供します 🤖

    const myMum = "Maria";
    if (true && typeof myMum === 'string') {
     console.log("My mum contains a string");
    } else {
     console.log("My mum is not a string");
    }
    


    先ほど見たように、My mum contains a string が出力されます.これは、2 つの条件が真であり、AND ロジック ポートを使用しているためです.
    よりよく理解するために、JS を使用していくつかの条件を練習することをお勧めします☺️その使用方法.

    switch-case 節



    複数の静的条件を確認し、その値に応じて決定を下すことができます.

    switch (condition) {
      case valor1:
        // It will run when the conditions is match `valor1` 
        [break;]
      case valor2:
        // It will run when the conditions is match `valor2` 
        [break;]
      ...
      case valorN:
        // It will run when the conditions is match `valorN` 
        [break;]
      default:
        // It will run when all the conditions are false
        [break;]
    }
    


    これには大きな力があり、複数の可能な値を簡単にチェックする機会をもたらします.代わりに、複数の if-else 句を使用できますが、コードが判読できなくなります🤒.多くの異なる値を取ることができる条件をチェックする必要がある場合、より多くの組織をもたらすようになります.例を見てみましょう:

    const foo = 0;
    switch (foo) {
      case -1:
        console.log('1 negative');
        break;
      case 0: // foo is 0, then the following block will be run
        console.log(0)
      break; // Break allow us not to run case1
      case 1: 
        console.log(1);
        break; // Break allow us not to run case2
      case 2:
        console.log(2);
        break;
      default:
        console.log('default');
    }
    


    さらに、同じアクションを実行する必要がある条件に対して同じことを実行できます.例を見てみましょう.

    const animal = 'giraffe';
    switch (Animal) {
      case 'Dog':
      case 'giraffe':
      case 'Cat':
      case 'Bird':
        console.log('This animal will live.');
        break;
      case 'elephant':
      default:
        console.log('This animal will not.');
    }
    


    先ほど見たように、4 つのケースのうちの 1 つが明らかになった場合は、同じアクションを実行します😇.

    ここでは、条件をチェックする方法と、式の可能な値について実行時にコードを決定する方法を見てきました.

    本日の投稿は以上です!!!

    私たちの投稿が本当に気に入った場合は、友達と共有することを忘れないでください.