JavaScript
Functions
A function (also known as a method) is a self-contained piece of code that performs a
particular "function". You can recognise a function by its format - it's a piece of
descriptive text, followed by open and close brackets.A function is a reusable code-
block that will be executed by an event, or when the function is called.
To keep the browser from executing a script when the page loads, you can put your
script into a function.
A function contains code that will be executed by an event or by a call to that
function.
You may call a function from anywhere within the page (or even from other pages if
the function is embedded in an external .js file).
Functions can be defined both in the and in the section of a document. However, to
assure that the function is read/loaded by the browser before it is called, it could be
wise to put it in the section.
Example
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" >
</form>
</body>
</html>
If the line: alert("Hello world!!") in the example above had not been put within a
function, it would have been executed as soon as the line was loaded. Now, the script
is not executed before the user hits the button. We have added an onClick event to
the button that will execute the function displaymessage() when the button is
clicked.
How to Define a Function
The syntax for creating a function is:
function functionname(var1,var2,...,varX)
{
code to be executed
}
var1, var2, etc are variables or values passed into the function. The { and the }
defines the start and end of the function.
Note: A function with no parameters must include the parentheses () after the
function name.
function functionname()
{
code to be executed
}
The return Statement
The return statement is used to specify the value that is returned from the function.
So, functions that are going to return a value must use the return statement.
Example
function prod(a,b)
{
x=a*b;
return x;
}
When you call the function above, you must pass along two parameters:
product=prod(2,3);
The returned value from the prod() function is 6, and it will be stored in the variable
called product.
The Lifetime of JavaScript Variables
When you declare a variable within a function, the variable can only be accessed
within that function. When you exit the function, the variable is destroyed. These
variables are called local variables. You can have local variables with the same name
in different functions, because each is recognized only by the function in which it is
declared.
If you declare a variable outside a function, all the functions on your page can access
it. The lifetime of these variables starts when they are declared, and ends when the
page is closed.
Strings:
• The String object is used to manipulate a stored piece of text.
• Strings can be created using single quotes (') or double quotes (")
• Template literals can be used to create strings with embedded expressions
using backticks (``)
String Properties and Methods
• length: returns the length of the string
• toUpperCase(): converts the string to uppercase
• toLowerCase(): converts the string to lowercase
• trim(): removes whitespace from the beginning and end of the string
• split(): splits the string into an array of substrings
• join(): joins an array of strings into a single string
String Concatenation
• Strings can be concatenated using the + operator
• Template literals can be used to concatenate strings with embedded
expressions
Note: We can insert variable directly in template literal . This is called string
interpolation.
Escaping Characters
• Backslash (\) is used to escape special characters in strings.