Вы находитесь на странице: 1из 29

In computer programming languages,

a switch statement is a type of selection


control mechanism used to allow the value of
a variable or expression to change the control
flowof program execution via a multiway
branch.
Switch statement in C example, in PHP, a constant can be used
as the "variable" to check against, and the
first case statement which evaluates to
switch (age) { that constant will be executed:
case 1: printf("You'reone."); switch(true) {
break; case ($x == 'hello'):
case 2: printf("You're two."); foo();
break; break;
case 3:printf("You're three."); case ($z == 'howdy'):
break; break;
case 4: printf("You're four."); }
switch(5) {
break; case $x: break;
default: printf("You're case $y: break;
neither!"); break; }
}
The for statement will execute a block of
code a specified number of times.

for (initialization; condition; increment) {


code to be executed
}
<html>
<body>
<script type="text/javascript">
for (i=0; i<=5; i++)
{
document.write("<b>The number is " + i + "</b>")
document.write("<br>")
}
</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>
loops through a block of code while a
condition is true

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.

Note: Without a label reference, the break statement can only be


used inside a loop or a switch.
Wala ng example ng Break alam nyo naman
na kung pano magbreak eh.
JavaScript functions are defined with
the function keyword.
You can use a function declaration or a
function expression.

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
}

Function parameters are the names listed in the


function definition.
Function arguments are the real values passed
to (and received by) the function.
Parameter Rules

JavaScript function definitions do not specify


data types for parameters.
JavaScript functions do not perform type
checking on the passed arguments.
JavaScript functions do not check the number
of arguments received.
If a function is called with missing arguments (less than declared),
the missing values are set to: undefined
Sometimes this is acceptable, but sometimes it is better to assign
a default value to the parameter:
Example:
function myFunction(x, y) {
if (y === undefined) {
y = 0;
}
}

If a function is called with too many arguments (more than


declared), these arguments can be reached using the arguments
object.
JavaScript functions can be invoked in 4 different ways.
Each method differs in how this is initialized.

The this Keyword:


In JavaScript, the thing called this, is the object that
"owns" the current code.
The value of this, when used in a function, is the object
that "owns" the function.
Note that this is not a variable. It is a keyword. You cannot
change the value of this.
You have already learned that the code inside a
JavaScript function will execute when "something"
invokes it.
The code in a function is not executed when the
function is defined. It is executed when the function
is invoked.
Some people use the term "call a function" instead of
"invoke a function".
It is also quite common to say "call upon a function",
"start a function", or "execute a function".
In this tutorial, we will use invoke, because a
JavaScript function can be invoked without being
called.
Example:
function myFunction(a, b) {
return a * b;
}
myFunction(10, 2); // myFunction(10, 2) will return 20
The function above does not belong to any object. But in JavaScript there is always a default global object.
In HTML the default global object is the HTML page itself, so the function above "belongs" to the HTML page.
In a browser the page object is the browser window. The function above automatically becomes a window function.
myFunction() and window.myFunction() is the same function:

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

Invoking a function as a global function, causes the value of this to


be the global object.
Using the window object as a variable can easily crash your
program.
JavaScript variables can belong to the local or global scope.
Private variables can be made possible with closures.
Global Variables
A function can access all variables defined inside the function, like
this:
Example:
function myFunction() {
var a = 4;
return a * a;
}

But a function can also access variables defined outside the


function, like this:
Example:
var a = 4;
function myFunction() {
return a * a;
}

In the last example, a is a global variable.


In a web page, global variables belong to the window object.
Global variables can be used (and changed) by all scripts in the page (and in the
window).
In the first example, a is a local variable.
A local variable can only be used inside the function where it is defined. It is
hidden from other functions and other scripting code.
Global and local variables with the same name are different variables. Modifying
one, does not modify the other.
Variables created without the keyword var, are always global, even if they are
created inside a function.
A regular expression is a sequence of characters that forms a
search pattern.
The search pattern can be used for text search and text replace
operations.
What Is a Regular Expression?
A regular expression is a sequence of characters that forms
a search pattern.
When you search for data in a text, you can use this search pattern
to describe what you are searching for.
A regular expression can be a single character, or a more
complicated pattern.
Regular expressions can be used to perform all types of text
search and text replace operations.
/pattern/modifiers;
Example:
var patt = /w3schools/i;

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);

The result in n will be:


6
The search method will also accept a string as
search argument. The string argument will be
converted to a regular expression:

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:

var str = "Visit Microsoft!";


var res = str.replace(/microsoft/i, "W3Schools");

The result in res will be:


Visit W3Schools!
The replace() method will also accept a string
as search argument:
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");

Did You Notice?


Regular expression arguments (instead of string
arguments) can be used in the methods above.
Regular expressions can make your search much more
powerful (case insensitive for example).
Thank you

Вам также может понравиться