Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning JavaScript Data Structures and Algorithms

You're reading from   Learning JavaScript Data Structures and Algorithms Hone your skills by learning classic data structures and algorithms in JavaScript

Arrow left icon
Product type Paperback
Published in Jun 2016
Publisher Packt
ISBN-13 9781785285493
Length 314 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Loiane Avancini Loiane Avancini
Author Profile Icon Loiane Avancini
Loiane Avancini
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. JavaScript—A Quick Overview FREE CHAPTER 2. Arrays 3. Stacks 4. Queues 5. Linked Lists 6. Sets 7. Dictionaries and Hashes 8. Trees 9. Graphs 10. Sorting and Searching Algorithms 11. Patterns of Algorithm 12. Algorithm Complexity

Control structures

JavaScript has a similar set of control structures as the C and Java languages. Conditional statements are supported by if...else and switch. Loops are supported by the while, do...while, and for constructs.

Conditional statements

The first conditional statement we will take a look at is the if...else construct. There are a few ways we can use the if...else construct.

We can use the if statement if we want to execute a script only if the condition is true, as follows:

var num = 1; 
if (num === 1) { 
  console.log("num is equal to 1"); 
} 

We can use the if...else statement if we want to execute a script and the condition is true or another script just in case the condition is false (else), as follows:

var num = 0; 
if (num === 1) { 
  console.log("num is equal to 1"); 
} else { 
  console.log("num is not equal to 1, the value of num is " + num); 
} 

The if...else statement can also be represented by a ternary operator. For example, take a look at the following if...else statement:

if (num === 1){ 
  num--; 
} else { 
  num++; 
} 

It can also be represented as follows:

(num === 1) ? num-- : num++; 

Also, if we have several scripts, we can use if...else several times to execute different scripts based on different conditions, as follows:

var month = 5; 
if (month === 1) { 
  console.log("January"); 
} else if (month === 2){ 
  console.log("February"); 
} else if (month === 3){ 
  console.log("March"); 
} else { 
  console.log("Month is not January, February or March"); 
} 

Finally, we have the switch statement. If the condition we are evaluating is the same as the previous one (however, it is being compared to different values), we can use the switch statement:

var month = 5; 
switch(month) { 
  case 1: 
  console.log("January"); 
  break; 
  case 2: 
  console.log("February"); 
  break; 
  case 3: 
  console.log("March"); 
  break; 
  default: 
  console.log("Month is not January, February or March"); 
} 

One thing that is very important in a switch statement is the usage of the case and break keywords. The case clause determines whether the value of switch is equal to the value of the case clause. The break statement stops the switch statement from executing the rest of the statement (otherwise, it will execute all the scripts from all case clauses below the matched case until a break statement is found in one of the case clauses). Finally, we have the default statement, which is executed by default if none of the case statements are true (or if the executed case statement does not have the break statement).

Loops

Loops are very often used when we work with arrays (which are the subject of the next chapter). Specifically, we will use the for loop in our algorithms.

The for loop is exactly the same as in C and Java. It consists of a loop counter that is usually assigned a numeric value, then the variable is compared against another value (the script inside the for loop is executed while this condition is true), and finally the numeric value is increased or decreased.

  • In the following example, we have a for loop. It outputs the value of i on the console, while i is less than 10; i is initiated with 0, so the following code will output the values 0 to 9:
        for (var i=0; i<10; i++) { 
          console.log(i); 
        } 
  • The next loop construct we will look at is the while loop. The script inside the while loop is executed while the condition is true. In the following code, we have a variable, i, initiated with the value 0, and we want the value of i to be output while i is less than 10 (or less than or equal to 9). The output will be the values from 0 to 9:
        var i = 0; 
        while(i<10) 
        { 
          console.log(i); 
          i++; 
        } 
  • The do...while loop is very similar to the while loop. The only difference is that in the while loop, the condition is evaluated before executing the script, and in the do...while loop, the condition is evaluated after the script is executed. The do...while loop ensures that the script is executed at least once. The following code also outputs the values from 0 to 9:
        var i = 0; 
        do { 
          console.log(i); 
          i++; 
        } while (i<10) 
You have been reading a chapter from
Learning JavaScript Data Structures and Algorithms - Second Edition
Published in: Jun 2016
Publisher: Packt
ISBN-13: 9781785285493
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image