0% found this document useful (0 votes)
4 views

Functions in Javascript

Uploaded by

ruhanplayz2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Functions in Javascript

Uploaded by

ruhanplayz2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

User defined functions


Function Definition Syntax:
function function_name(parameter1, parameter2, parameter3)
{
// code to be executed
}

A JavaScript function is defined with the function keyword,

followed by a name, [Function names can contain letters, digits,


underscores, and dollar signs (same rules as variables).]

followed by parentheses () -parentheses may include parameter names


separated by commas

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

• When it is invoked (called) from JavaScript code


• When an event occurs (when a user clicks a button)
• Automatically (self invoked)

Calling Functions Syntax:


After defining a function, the next step is to call them to make use of the
function.
We can call a function by using the function name separated by the value
of parameters enclosed between the parenthesis and a semicolon at the end.
functionName(Value1, Value2, ..);
Why? Or Advantages:

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.
• Program Format: for When it is invoked (called) from JavaScript
code

Example 1:[Try it in Ur system] Function without parameters.

m,,,,,,,,,,,,,,,,,,,vcvmbv,n
,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,
mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,mnbv,m
nbv,mnbv,mnbv,mnbv,mnbvvbbbbbbbbbbbb

Example 2:[Try it in Ur system] Function with parameters.


• Program Format: For When an event occurs (when a user clicks a
button) code

Example 3:[Try it in Ur system] Function without parameters using button


click
WITH DOM(DOCUMENT OBJECT MODEL) METHODS:

DOM methods are actions you can perform (on HTML Elements).

DOM properties are values (of HTML Elements) that you can set or
change.

The getElementById Method- This method is used when


developers have defined certain HTML elements with IDs that
uniquely identify the same elements in the whole document. The
innerHTML Property - to get the content of an element
Example 4:[Try it in Ur system] Function without parameters using
button click with DOM methods

getElementsByName() returns all the elements having the given name value.

getElementsByTagName() returns all the elements having the given tag name.

getElementsByClassName() returns all the elements having the given class name.

You might also like