Академический Документы
Профессиональный Документы
Культура Документы
while (condition)
{
code to be executed
}
<html>
<body>
<script type="text/javascript">
i=0 while (i<=5)
{
document.write("<b>The number is " + i + "</b>")
document.write("<br>")
i++
}
</script>
<p>Explanation:
<p>The for loop sets <b>i</b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 5, the loop will continue to
run.
<p><b>i</b> will increase by 1 each time the loop runs.
</body>
</html>
The break statement exits a switch statement or a loop (for, for ...
in, while, do ... while).
When the break statement is used with a switch statement, it
breaks out of the switch block. This will stop the execution of more
execution of code and/or case testing inside the block.
When the break statement is used in a loop, it breaks the loop and
continues executing the code after the loop (if any).
The break statement can also be used with an optional label
reference, to "jump out" of any JavaScript code block.
Function Declarations:
function functionName(parameters) {
code to be executed
}
A JavaScript function does not perform any
checking on parameter values (arguments).
Function Parameters and Arguments:
functionName(parameter1, parameter2, parameter3) {
code to be executed
}
Example:
function myFunction(a, b) {
return a * b;
}
window.myFunction(10, 2); // window.myFunction(10, 2) will also return 20
This is a common way to invoke a JavaScript function, but not a very good practice.
Global variables, methods, or functions can easily create name conflicts and bugs in the global object.
When a function is called without an owner object, the value
of this becomes the global object.
In a web browser the global object is the browser window.
This example returns the window object as the value of this:
Example
function myFunction() {
return this;
}
myFunction(); // Will return the window object
Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-
insensitive).
In JavaScript, regular expressions are often
used with the two string methods: search()
and replace().
The search() method uses an expression to
search for a match, and returns the position
of the match.
The replace() method returns a modified
string where the pattern is replaced.
Example:
Use a regular expression to do a case-
insensitive search for "w3schools" in a string:
var str = "Visit W3Schools";
var n = str.search(/w3schools/i);
Example:
Use a string to do a search for "W3schools" in
a string:
var str = "Visit W3Schools!";
var n = str.search("W3Schools");
Example:
Use a case insensitive regular expression to
replace Microsoft with W3Schools in a string: