Expressions and statements
A fragment of code that produces a value is called an
Every value that is written literally is an expression. An expression between parentheses is also an expression, as is a binary operator applied to two expressions or a unary operator applied to one. The simplest kind of statement is an expression with a semicolon after it. The = operator can be used at any time on existing variables to disconnect them from their current value and have them point to a new one.
The environment
The collection of variables and their values that exist at a given time is called the environment. When a program starts up, this environment is not empty. It always contains variables that are part of the language standard, and most of the time, it has variables that provide ways to interact with the surrounding system. For example, in a browser, there are variables and functions to inspect and influence the currently loaded website and to read mouse and keyboard input.
A lot of the values provided in the default environment have the type function. A
function is a piece of program wrapped in a value.
Such values can be applied in order to run the wrapped program. For example, in a browser environment, the variable alert holds a function that shows a little dialog box with a message. Executing a function is called invoking, calling, or applying it. You can call a function by putting parentheses after an expression that produces a function value.
The console.log function
The alert function can be useful as an output device when experimenting, but clicking away all those little windows will get on your nerves. Most JavaScript systems (including all modern web browsers and Node.js) provide a console.log function that writes out its arguments to some text output device. In browsers, the output lands in the JavaScript console. This part of the browser interface is hidden by default,but most browsers open it when you press F12. var x = 30; console.log("the value of x is", x); // the value of x is 30
Return values
When a function produces a value, it is said to return that value. Anything that produces a value is an expression in JavaScript, which means function calls can be used within larger expressions. Here a call to Math.min, which is the opposite of Math.max, is used as an input to the plus operator: console.log(Math.min(2, 4) + 100); // 102
Prompt and confirm
You can ask the user an OK/Cancel question using confirm. This returns a Boolean: true if the user clicks OK and false if the user clicks Cancel.
Control flow
When your program contains more than one statement, the statements are executed, predictably, from top to bottom.
Conditional execution
Executing statements in
order isnĀ“t the only option we have. An alternative is
conditional execution
, where we choose between two different routes based on a Boolean value var theNumber = Number(prompt("Pick a number", "")); if (!isNaN(theNumber)) alert("Your number is the square root of " + theNumber * theNumber); With this modification, if you enter cheese, no output will be shown. The keyword if executes or skips a statement depending on the value of a Boolean expression. The deciding expression is written after the keyword, between parentheses, followed by the statement to execute. var num = Number(prompt("Pick a number", "0")); if (num < 10) alert("Small"); else if (num < 100) alert("Medium"); else alert("Large");
While and do loops
Looping control flow allows us to go back to some point in the program where we were before and repeat it with our current program state. If we combine this with a variable that counts, we can do something like this: var number = 0; while (number <= 12) { console.log(number); number = number + 2; } var number = 0; while (number <= 12) { console.log(number); number = number + 2; }
Indenting Code
In JavaScript, these are not required. The role of the indentation inside blocks is to make the structure of the code stand out.
for loops
for loop The for loop has the following syntax: for (statement 1; statement 2; statement 3) { code block to be executed } Statement 1 is executed before the loop (the code block) starts. Statement 2 defines the condition for running the loop (the code block). Statement 3 is executed each time after the loop (the code block) has been executed. for (i = 0; i < 5; i++) { text += "The number is " + i + "
"; }
Breaking Out of a Loop
has the effect of immediately jumping out of the enclosing loop. Using the remainder (%) operator is an easy way to test whether a number is divisible by another number. If it is, the remainder of their division is zero.
Updating variables succinctly
Especially when looping, a program often needs to update a variable to hold a value based on that ariables previous value. counter = counter + 1; JavaScript provides a shortcut for this: counter += 1;
Most JavaScript programmers capitalize every word except the first.