雄弁なJavaScript :第2章


このブログでは、本の第2章で学んだことをカバーします.

目次


  • Chapter 1
  • Expressions and statements
  • Variables
  • Functions
  • Control Flow
  • Break, Continue
  • Switch
  • 第2章

    式と文

    Expressions are the fragments of code that produces a value. Every value is an expression.
    Statements are the complete sentences that make some sense ,both to humans and computers.
    A program is a list of statements grouped together to get the desired output.
    Therefore,
    Expressions->Statements->Programs



    変数

    Variables, also known as bindings are a way to store the values we want to apply calculations upon. Like, we humans need a copy and pen to write down a value and then perform some calculations on it, similarly computers have memory to store the numbers and then perform the calculations we want them to. This is done via variables or bindings. So, variables let us store numbers, strings, result, anything.

    var myName = "Sakshi";
    console.log(myName);
    

    We can declare a binding either using let, var or const keywords.
    They all give almost same result apart from the fact that const is used mostly when we do not want the value of out binding to change, i.e. the value remain constant throughout the program.
    We can change the values provided to variables using 'var' and 'let' keywords.
    var : Variable
    const : Constant

    var mySaving = 400;
    //if I receive 100Rs this month 
    mySaving = mySaving+100;
    console.log(mySaving);
    
    //result : 500
    

    If we try to modify a const value during the tenure of a program, we will get an error message!

    The variable name could be anything according to our convenience. Although it must not start with a number. Also, we will get error message if we try to name our variable similar to the name of some keyword like : let, break, const, etc.



    関数

    Functions are nothing but the piece of programs wrapped in a value. It is advisable to use functions in out program as, if once declared they can be used multiple times (otherwise we would have to write the entire code again and again).

    //add two numbers
    function add(a,b)   //function declaration
    {  
      var sum = a+b;
      return sum;
    }
    
    var ans = add (4,5);  //function call
    console.log("The sum is "+ ans);
    
    result : The sum is 9
    
    

    Function parameters are the names listed in the function's definition(in this example : a,b). Function arguments are the real values passed to the function.
    There are some predefined functions like console.log() and some functions that are user defined - add(), in this case.

    console.log() : is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. It prints the output in the console of the browser.

    return : The return statement stops the execution of a function and returns a value from that function. The add() function returns the value of sum.



    制御フロー

    We can have straight-line execution or conditional execution in our programs.
    (A)IF ELSE LOOPS:
    The conditional execution can be done using IF-ELSE loop.

    //straight line
    var age = 34;
    console.log(age);
    
    //conditional
    var age = 34;
    if( age<30)
      console.log("My age is " +age);
    else
      console.log("I am older than 30. Age : " + age);
    
    

    Since there can be multiple conditions, we can use IF-ELSEIF-ELSE loops.

    if (condition1)
      statement1
    else if (condition2)
      statement2
    else if (condition3)
      statement3
    ...
    else
      statementN
    
    

    (B) WHILE and DO LOOPS :

    While loop is used when we want to execute certain statements multiple times.

    var age = 10;
    while(age < 15)  //this condition checks if age is less than 15. 
                       If true the inner loop executes.
    {
      console.log(age);
      age = age + 1;
    }
    
    //result : 10 11 12 13 14
    

    The DO-WHILE loop executes at least once for sure !

    let yourName;
    do {
    yourName = prompt("Who are you?");
    } while (!yourName);
    console.log(yourName);
    

    This program will force you to enter a name. It will ask again and again until it gets something that is not an empty string. This means the loop continues going round until you provide a non-empty name.


    ループ用
    時々ループが混乱することができますので、ループのために救助に来る.彼らは繰り返し、あるステートメントを通してループする同じ機能を実行する.
    for (statement 1; statement 2; statement 3) {
      // code block to be executed
    }
    
    文1はコードブロックの実行前に1回実行されます.
    ステートメント2はコードブロックを実行するための条件を定義します.条件が偽ならば、我々はループから出ます.
    文3はコードブロックを実行した後(毎回)実行されます.
    for( var i = 0 ; i < 5 ; i++ )
    {
      console.log(i);
    }
    
    
    //result : 0 1 2 3 4
    

    休憩と継続

    The break statement "jumps out" of a loop. It breaks the loop and continues executing the code after the loop (if any).

    for (var i = 0; i < 10; i++) {
      if (i === 3) 
        {  
            break;   //breaks out of the for loop
        }
      console.log(i);
    }
    
    
    //result : 0 1 2
    

    The continue statement "jumps over" one iteration in the loop. It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

    for (var i = 0; i < 6; i++) {
      if (i === 3) 
        {  
            continue;   //goes back to the for loop
        }
      console.log(i);
    }
    
    //result : 0 1 2 4 5
    


    スイッチ

    The switch statement is used to perform different actions based on different conditions.

    switch(expression) {
      case x:
        // code block
        break;
      case y:
        // code block
        break;
      default:
        // code block
    }
    


    camelCase:

    The standard JavaScript functions, and most JavaScript programmers, follow the camelCase style—they capitalize every word except the first.

    var myName = "Sakshi";
    var newAdditionNumber = 23;
    

    IMPORTANCE OF IDENTATION :

    -Easier to read
    -Easier to understand
    -Easier to modify
    -Easier to maintain
    -Easier to enhance

    IMPORTANCE OF COMMENTS :

    -When people work together, comments make it easier for others to read and understand your code.
    -If we want to see/edit code later, comments help us to memorize the logic that was written while writing that code.



    Thankyou for reading!😃
    All feedbacks are welcome 🙆‍♀️

    Connect with me on :

  • Github