



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




JavaScript Functions
A function is a group of reusable code which can be called anywhere in your program. It’s a
block of code designed to perform a particular task. This eliminates the need of writing the same
code again and again. It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions. A
function is executed when something invokes it.
Like any other advanced programming language, JavaScript also supports all the features
necessary to write modular code using functions.
Function Definition
Before we use a function, we need to define it. The most common way to define a function in
JavaScript is by using the function keyword, followed by a unique function name, a list of
parameters (that might be empty), and a statement block surrounded by curly braces.
Consider the following function:
Calling a function
To invoke a function somewhere later in the script, you would simply need to write the name of
that function
Click the following button to call the function
Function with Parameters
A function can take multiple parameters separated by comma. You can pass different parameters
while calling a function. These passed parameters can be captured inside the function and any
manipulation can be done over those parameters.
Click the following button to call the function
Use different parameters inside the function and then try...
Functions that returns a value
A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function. For example, you can pass two numbers in a function and then you can expect the function to return their multiplication in your calling program. Example
Onblur- triggers when the window loses foc
Ondrag- tiggers when an element is dragged.
Onfocus- triggers when the window gets focus
Onkeydown- triggers when a key is pressed
Onkeypress- triggers when a key is pressed and released
Onkeyup- triggers when a key is released
Onresize- triggers when a window is resized
Onscroll- triggers when an element scrollbar is being scrolled
Onselect- triggers when an element is selected
Onclick example:
function swap(colour) { if (colour == "Green") { document.bgColor = "Green" } else { document.bgColor = "Yellow" } }
Onmouseover and onmouseout example
Our services
Dialog boxes
JavaScript supports three important types of dialog boxes. These dialog boxes can be used to raise and alert, or to get confirmation on any input or to have a kind of input from the users.
An alert box is often used to if you want to make sure information comes through to the user. An alert dialog box is mostly used to give a warning message to the users. However, an alert box can still be used for friendlier messages. Alert box gives only one button "OK" to select and proceed.
The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus, it enables you to interact with the user. The user needs to fill in the field and then click OK. This dialog box is displayed using a method called prompt() which takes two parameters: (i) a label which you want to display in the text box and (ii) a default string to display in the text box. This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window method prompt() will return the entered value from the text box. If the user clicks the Cancel button, the window method prompt() returns null.