Categories
JavaScript Basics

Highlights of JavaScript — Variables, Switch, and While Loops

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Local vs Global Variables

JavaScript has local and global variables.

Local variables are accessible only within a block.

And global variables are available throughout the app.

With JavaScript, we create local variables with the let and const keywords.

They are all block-level, so they’re all only available within a block like functions, and if blocks.

let creates a variable, and const creates constants.

Constants can’t be reassigned a new after it’s assigned a value.

But the content may still change.

Constants must have an initial value assigned to it when it’s created.

For example, if we have:

function addNumbers() {  
  let theSum = 2 + 2;  
}

The theSum is only available within the addNumbers function.

But if we have:

function addNumbers() {  
  theSum = 2 + 2;  
}

then theSum is a global variable.

We can access the value anywhere.

If a variable is global, then it’s attached property of the window global object.

We should avoid global variables since it’s easy to create global variables with the same name.

Therefore, it’s easy to have conflicts.

And it’s hard to track how variables are changed.

Switch Statements

The switch statement lets us do different things for different cases in one statement.

For example, we can write:

switch (dayOfWk) {  
  case "Sat":  
    alert("very happy");  
    break;  
  case "Sun":  
    alert("happy");  
    break;  
  case "Fri":  
    alert("happy friday");  
    break;  
  default:  
    alert("sad");  
}

We check the value of dayOfWk to let us do different things when it has the given values.

If dayOfWk is 'Sat' , then we show a 'very happy' alert.

If dayOfWk is 'Sun' , then we show a 'happy' alert.

If dayOfWk is 'Fri' , then we show a 'happy friday' alert.

The default clause is run when dayOfWk has any other value, and we show the 'sad' alert.

The break keyword is required so that the rest of the switch statement won’t run after a case is found.

The switch statement above is the same as:

if (dayOfWk === "Sat") {  
  alert("very happy");  
} else if (dayOfWk === "Sun") {  
  alert("happy");  
} else if (dayOfWk === "Fri") {  
  alert("hapopy friday");  
} else {  
  alert("sad");  
}

default in the switch statement is the same as the else in the if statement.

While Loops

The JavaScript while loop is another kind of loop that we may use.

It lets us run a loop when a given condition is true .

For example, we can write:

let i = 0;  
while (i <= 3) {  
  console.log(i);  
  i++;  
}

In the loop above, when i is less than or equal to 3, then we run the loop.

Inside the loop body, we update i so that we work toward ending the loop.

We indent the loop block with 2 spaces for easy typing and readability.

Conclusion

JavaScript has a while loop to let us repeatedly run code.

Also, we should use local variables and avoid global variables.

switch statements lets us run code given some value of a variable.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *