A Walkthough of JavaScript

Conditionals

If Statements


    let isOne = true;

    if(isOn == true){
        console.log("The light is on!");
    };
                    

An "if statement" is used to run a block of code based on whether a condition is true or not. notice, that 'if' is lowercase. This is usually used if you need your program to make a decision.

Else Statements


    let weather = 75;

    if(weather < 70){
        console.log("Wear a jacket");
    } else {
        console.log("No jacket necessary!");
    };
                    

Similar to the "if statment", this type of code is used whether a condition is true or not, however, this code has an alternative set of instructions, based on whether another factor in your code is true or false.

Else If Statements


    let fb = 100;

    if(fb % 3 == 0 && fb % 5 == 0){
        console.log(FizzBuzz");
    } else if(fb % 3 == 0){
        console.log("Fizz");
    } else if(fb % 5 == 0){
        console.log("Buzz");
    } else{
        console.log(fb);
    };
                        

This is an extension of the "else statement". This type of code has many branches of statements available to run, based on what factors may be true in the code.

Switches


    var friend = "Brandon"

    switch(friendTwo) {
        case "Brandon":
            console.log("That is my name!");
            break;
        case "Justis":
            console.log("You are my desk neighbor");
            break;
        case "Kito":
            console.log("You play Warhammer 40k");
            break;
        case "Sean":
            console.log("You're bald!");
            break;
        default:
            console.log("Its nice to meet you!");
    };
                    

This is alternative form of conditionals. Great for use on items that have many conditions. The default line at the end is a catchall: whatever doesn't work for the rest of the conditional, gets executed here.

Ternary


    let x = 14;

    (x >= 10) ? console.log(true) : console.log(false);
                    

Ternary is a great way to write an if statement in a single line. Ususally used when only a few outcomes are required, but can be used for any amount.



Loops

For Loop


    for(var i = 0; i < 10; i++){
        console.log(i);
    };
                        

A for loop is used to repeat a task several times over, provided you have a known end goal. This is much better than having to type out the same task 50, 100, 300, or even more times in a row!

While Loop


    while(i < 10){
        console.log(i);
        i++;
    };
                        

The While loops is a loop used when you don't know how many iterations you want, but you know you need to run something several times. Used with a boolean as a condition, this loop will run until the condition is false.

Do While Loops


    let i = 0;

    do{
        console.log(i);
        i++;
    }
    while(i >= 10)
                        

Similar to the while loop, this loop also runs on a boolean condition. This loops is different, however, because this will run at least once before the condition is checked to be either true, or false. This means that this loop will always execute at least one time.

For In Statement


    let catArray = ["Tabby", "Burmese", "Ragdoll", Tortie"];

    for(cat in catArray){
        console.log(cat);
        console.log(catArray[cat]);
    };
                        

Used in conjunction with arrays, for in statments are typically used to grab the index location for an item in the array. This can however be used to grab the actual item, as long as console.log is used.

For Of Statement


    let catArray = ["Tabby", "Burmese", "Ragdoll", Tortie"];
                                
    for(cat of catArray){
        console.log(cat);
        console.log(catArray[cat]);
    };
                            

Very similar to the For In Statement, the For Of Statement just reverses the logic, meaning, instead of pulling the index location, this loop will pull the actual item.

For Each Statement


    const petArray = ["cat", "dog", "fish"];

    petArray.forEach(pet => {
        console.log(pet);
    });
                            

A for each loop is similar to a for of statement, in that it pulls items out of seperate indexes in an array.>

Break Statement


    for(i = 0; i < 10; i++){
        if(i === 3){break;}
            console.log(`The number i s${i}`);
    };
                            

Used to end the function of a loop, break statements are typically used once a condition is met. This statement is also used in switch statements, to stop the execution of the rest of the statement.

Contintue Statement


    for(i = 0; i < 10; i++){
        if (i === 3){continue;}
        console.log(`Your number is ${i}`);
    };
                            

Similar to the break statement, a continue statement stops a loop from running, but only for a single iteration. In this instance, the loop will skip its fourth iteration (the iteration where i = 3).



Functions

Standard Functions


    function counting(){
        for(i = 1; i <= 10; i++){
            console.log(i);
        }
    };
    counting();
                            

Functions allow a programmer to write one block of code, and then call it as many times as they wish throughout the program, without having to rewrite it. This is an extremely useful tool, as it allows faster coding, and a cleaner program.

Fat Arrow Functions


    let name = (firstName) =>
    console.log(`${firstName}, is what they call me`);

    name("Brandon");
                            

Just like a normal function, a fat arrow function can be called later in the code. These functions however, are best used for simple functions, with only one or two tasks.



Arrays


    let favMovies = ["Alien", "Aliens", "Alien Resurection"];
                                

Arrays, are essentially a list of information that is all held within one variable. They are very useful in conjunction with loops to store an enormous amount of data, relatively easily.



Objects


    let garden = {
        vegetable: "zucchini",
        flower: "sun flower",
        fruit: "grape",
        water: true,
        sun: true,
        size: 10
    };
                                    

Objects are just big containers that hold related information. This information could be any data type, including arrays, and other objects. Objects are used in databases to hold together all the information about specific items.