0% found this document useful (0 votes)
5 views9 pages

JavaScript Functions

Uploaded by

Bhaskar K S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views9 pages

JavaScript Functions

Uploaded by

Bhaskar K S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task.

A JavaScript function is executed when "something" invokes it (calls it).

Example

<html>

<body>

<h1>JavaScript Functions</h1>

<p>Call a function which performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>

function myFunction(p1, p2) {

return p1 * p2;

let result = myFunction(4, 3);

document.getElementById("demo").innerHTML = result;

</script>

</body>

</html>
JavaScript Function Syntax
A JavaScript function is defined with the function keyword, followed by a name, followed
by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables).

The parentheses may include parameter names separated by commas:


(parameter1, parameter2, ...)

The code to be executed, by the function, is placed inside curly brackets: {}

function name(parameter1, parameter2, parameter3) {


// code to be executed
}

Function parameters are listed inside the parentheses () in the function definition.

Function arguments are the values received by the function when it is invoked.

Inside the function, the arguments (the parameters) behave as local variables.

Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:

 When an event occurs (when a user clicks a button)


 When it is invoked (called) from JavaScript code
 Automatically (self invoked)

You will learn a lot more about function invocation later in this tutorial.

Function Return
When JavaScript reaches a return statement, the function will stop executing.

If the function was invoked from a statement, JavaScript will "return" to execute the code
after the invoking statement.

Functions often compute a return value. The return value is "returned" back to the "caller":

Example
Calculate the product of two numbers, and return the result:

<html>

<body>

<h1>JavaScript Functions</h1>

<p>Call a function which performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>

let x = myFunction(4, 3);

document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {

return a * b;

</script>

</body>

</html>
Why Functions?
With functions you can reuse code

You can write code that can be used many times.

You can use the same code with different arguments, to produce different results.

The () Operator
The () operator invokes (calls) the function:

Example
Convert Fahrenheit to Celsius:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Functions</h1>

<p>Invoke (call) a function that converts from Fahrenheit to


Celsius:</p>
<p id="demo"></p>

<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
let value = toCelsius(77);
document.getElementById("demo").innerHTML = value;
</script>

</body>
</html>
JavaScript Regular Expressions
❮ PreviousNext ❯

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.

Syntax
/pattern/modifiers;

Example
/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).

Using String Methods


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.

Using String search() With a String


The search() method searches a string for a specified value and returns the position of the
match:

Example
Use a string to do a search for "W3schools" in a string:

let text = "Visit W3Schools!";


let n = text.search("W3Schools");

The result in n will be:

Using String search() With a Regular


Expression
Example
Use a regular expression to do a case-insensitive search for "w3schools" in a string:

let text = "Visit W3Schools";


let n = text.search(/w3schools/i);
The result in n will be:

Using String replace() With a String


The replace() method replaces a specified value with another value in a string:

let text = "Visit Microsoft!";


let result = text.replace("Microsoft", "W3Schools");

Regular Expression Patterns


Brackets are used to find a range of characters:

Expression Description

[abc] Find any of the characters between the brackets

[0-9] Find any of the digits between the brackets

(x|y) Find any of the alternatives separated with |

Metacharacters are characters with a special meaning:

Metacharacter Description Try


it

\d Find a digit Try it


»

\s Find a whitespace character Try it


»
Try it
\b Find a match at the beginning of a word like this: \bWORD, or at the »
end of a word like this: WORD\b Try it
»

\uxxxx Find the Unicode character specified by the hexadecimal number Try it
xxxx »

Quantifiers define quantities:

Quantifier Description Try it

n+ Matches any string that contains at least one n Try it »

n* Matches any string that contains zero or more Try it »


occurrences of n

n? Matches any string that contains zero or one Try it »


occurrences of n

Using the RegExp Object


In JavaScript, the RegExp object is a regular expression object with predefined properties
and methods.

Using test()
The test() method is a RegExp expression method.

It searches a string for a pattern, and returns true or false, depending on the result.

The following example searches a string for the character "e":


Example
const pattern = /e/;
pattern.test("The best things in life are free!");

Since there is an "e" in the string, the output of the code above will be:

true

You might also like