Skip to main content

Command Palette

Search for a command to run...

Loops,Scope and variable declaration in Javascript

Updated
•4 min read•View as Markdown
Loops,Scope and variable declaration in Javascript
D

Hi, I’m Divyanshi, a who loves building products. I’d love to combine my passion for learning with my software development skills to continue building personalized learning products for people.I am systematically organized, meticulous, diligent and conscientious self-starter, able to prioritize, plan and strategize effectively to accomplish multiple tasks and stay calm under pressure.With my prior knowledge and experience of project management and timed-completion, I move forward to discover and learn diversified challenges in the field of technology that just makes me learn more. Due to the diversity of our clients which included technology corporation,medical industry,foundations,I learn to adapt to changing environments,quickly learning new content & collaborate with people with different communication skills. Skills:Html, Css, Jquery, Javascript, Express, Node.js, Webpack, Basic AEM Fundamentals

Loops will iterate over values and repeatedly run a block of code.

While loop

  • They repeat an action some number of times.
  • Three main pieces of information that any loop should have are: When to start: The code that sets up the loop — defining the starting value of a variable for instance. When to stop: The logical condition to test whether the loop should continue. How to get to the next item: The incrementing or decrementing step — for example, x = x * 3 or x = x - 1
var x=0;
while(x<10){
console.log(x);
x=x+2; 
}
Prints:
0
2
4
6
8

If a loop is missing any of these three things, then you might find yourself in trouble. For instance, a missing stop condition can result in a loop that never ends!

Don't run this code!
while (true) {
  console.log("true is never false, so I will never stop!");
}

If you did try to run that code in the console, you probably crashed your browser tab.

Here's an example where a loop is missing how to get to the next item; the variable x is never incremented. x will remain 0 throughout the program, so the loop will never end.

Don't run this code!
var x = 0;
while (x < 1) {
  console.log('Oops! x is never incremented from 0, so it will ALWAYS be less than 1');
}

This code will also crash your browser tab, so we don't recommend running it.

For Loop

The for loop explicitly forces you to define the start point, stop point, and each step of the loop.

for (var i = 0; i < 8; i = i + 1) {
  console.log("Printing out i = " + i);
}
Prints:
Printing out i = 0
Printing out i = 1
Printing out i = 2
Printing out i = 3
Printing out i = 4
Printing out i = 5
Printing out i = 6
Printing out i = 7

Nested Loops

Nest loops inside of each other

for (var x = 0; x < 5; x = x + 1) {
  for (var y = 0; y < 3; y = y + 1) {
    console.log(x + "," + y);
  }
}
Prints:
0, 0
0, 1
0, 2
1, 0
1, 1
1, 2
2, 0
2, 1
2, 2
3, 0
3, 1
3, 2
4, 0
4, 1
4, 2

Scope and Variable Declaration

There are ways to declare a variable, by using let, const or var. To understand the difference between the three, first you need to understand the term scope.

What is a "Scope"?

The scope is defined as a specific portion of the code. There are three types of scope in Javascript

  1. Global scope - When a particular variable is visible (can be used) anywhere in the code. Such a variable is generally called as Global variable.
  2. Function scope - When a particular variable is visible (can be used) within a particular function only. Such a variable is generally called as Local variable.
  3. Block scope - When a particular variable is visible (can be used) within a pair of { . . . } only.
/*
 * Global scope. 
 * This variable declared outside of any function is called Global variable. 
 * Hence, you can use this anywhere in the code
 */
var opinion = "This course is amazing";

// Function scope
function showMessage() {
    // Local variable, visible within the function `showMessage`
    var message = "I am a Developer!"; 

    // Block scope
    {
          let greet = "How are you doing?";
        /*
         * We have used the keyword `let` to declare a variable `greet` because variables declared with the `var` keyword can not have Block Scope. 
         */
    } // block scope ends

    console.log( message ); // OK
    console.log( greet ); // ERROR. 
    // Variable greet can NOT be used outside the block

    console.log( opinion ); // OK    to use the gobal variable anywhere in the code

} // function scope ends

Variable Declaration

There are three ways to declare a variable:

  1. let - It a new way to declare a variable in any scope - Global, Local, or Block. The value of this variable can be changed or reassigned anytime within its scope.
  2. const - It is also a way to declare constants in any scope - Global, Local, or Block. Once you are assigned a value to a const variable, the value of this variable CANNOT be changed or reassigned throughout the code.
  3. var - This is the old way of declaring variables in only two scope - Global, or Local. Variables declared with the var keyword can not have Block Scope. The value of this variable can be changed or reassigned anytime within its scope.

More from this blog