JavaScript: Fundamental Programming Constructs
Overview
Through the prerequisite programming course requirements for this course, you have established a foundational understanding of the common, programming building blocks or constructs including operators, expressions, decision structures, looping, and functions. This activity focuses on a few of the common control structures in programming.
Associated Course Learning Outcomes
Demonstrate proficiency with JavaScript language syntax.
Prepare
Some of the common controls structures or building blocks in JavaScript include:
This is not an exhaustive list of JavaScript programming constructs. This is a list of some of the most common used. You will learn more about these constructs and others as you apply them throughout the course.
- Conditional Statements:
if
statement: Executes a block of code if a specified condition is true.if (condition) { // Code to execute if the condition is true }
The condition is evaluated to a boolean value, i.e., true or false. Writing condition statements is a critical skill in programming. The use of operators and expressions is a key concept in writing condition statements that solve programming problems.else
statement: Provides an alternative block of code to execute if the condition in the if statement is false.if (condition) { // Code to execute if the condition is true } else { // Code to execute if the condition is false }
else if
statement: This structure allows for the checking of multiple conditions in sequence.if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the conditions are true }
- Switch Statements: Provides a way to execute selective blocks of code
based on the value of an expression.
switch (expression) { case value1: // Code to execute if expression is equal to value1 break; case value2: // Code to execute if expression is equal to value2 break; // ... more cases ... default: // Code to execute if none of the cases match }
- Looping Statements:
for
Loop: Repeats a block of coe a specified number of times.for (let i = 0; i < 19; i++) { // Code to execute in each iteration }
while
Loop: Repeats a block of code as long as a specified condition is true.while (condition) { // Code to execute while the condition is true }
forEach
Loop: This loop structure is for arrays. It iterates over each element of the array.array.forEach(function(element) { // Code to execute for each element });
Activity Instructions
- Ponder: Repitition Structures