Another type of while loop is the do/while loop. This is useful if you always want to execute the code in the loop at least once and the expression cannot be tested until the code has executed at least once.
For example, the following do/while loop executes until days is equal to Wednesday:
Click here to view code image
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
The most basic type of looping in JavaScript is the while loop. A while loop tests an expression and continues to execute the code contained in its {} brackets until the expression evaluates to false.
For example, the following while loop executes until i is equal to 5:
A JavaScript for loop enables you to execute code a specific number of times by using a for statement that combines three statements in a single block of execution. Here’s the syntax:
Another type of for loop is the for/in loop. The for/in loop executes on any data type that can be iterated. For the most part, you will use for/in loops on arrays and objects. The following example illustrates the syntax and behavior of the for/in loop on a simple array:
Click here to view code image
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
You define a function by using the function keyword followed by a name that describes the use of the function, a list of zero or more arguments in (), and a block of one or more code statements in {}. For example, the following is a function definition that writes "Hello World" to the console:
Click here to view code image
function myFunction(){
console.log("Hello World");
}
To execute the code in myFunction(), all you need to do is add the following line to the main JavaScript or inside another function:
Frequently you need to pass specific values to functions, and the functions will use those values when executing their code. You pass values to a function in comma-delimited form. A function definition needs a list of variable names in () that match the number being passed in. For example, the following function accepts two arguments, name and city, and uses them to build the output string:
Click here to view code image
function greeting(name, city){
console.log("Hello " + name);
console.log(". How is the weather in " + city);
}
To call the greeting() function, you need to pass in a name value and a city value. The value can be a direct value or a previously defined variable. To illustrate this, the following code executes the greeting() function with a name variable and a direct string for city:
You can include more than one return statement in the function. When the function encounters a return statement, code execution of the function stops immediately. If the return statement contains a value to return, that value is returned. The following example shows a function that tests the input and returns immediately if it is zero:
For example, the following code defines a function doCalc() that accepts three parameters. The first two should be numbers, and the third is a function that will be called and passed the two numbers as arguments: