JavaScript Functions
Chapter 16 · Tamil Nadu State Board
- 4 min read
- 6-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains how JavaScript functions group code for a particular task and can be invoked repeatedly. It describes pre-defined functions and user-defined functions, including parameters, arguments and return values. Functions support reuse and clarity, while user-defined functions can divide larger programs into modules. Examples show functions checking values and responding to answers in an online quiz.
Written by AI from this lesson. How we check it
Key points
- A function groups JavaScript code for a task and can be invoked repeatedly.
From your textbook
“A function is a block of JavaScript code that is defined once but may be executed or invoked any number of times.”
- Functions help avoid repeated code and make programs clearer and more reusable.
From your textbook
“they are used to keep code that performs a particular job in one place in order to enhance reusability and program clarity.”
- Parameters act as local variables, and function invocations supply their values as arguments.
From your textbook
“parameters that work as local variables for the body of the function. Function invocations provide values or arguments”
- JavaScript functions are either pre-defined library functions or user-defined functions.
From your textbook
“Pre-defined or Library Functions • User-defined Functions”
- User-defined functions let programmers divide larger programs into smaller modules.
From your textbook
“User-defined functions allow the programmer to modularize a program.”
- A function body is enclosed in braces, and a function can return a value.
From your textbook
“The function body must be enclosed by braces. Example: function sum(x,y) { var m=x+y; return m; }”
- The isNaN() function returns true when a supplied value is not a number.
From your textbook
“This function returns true if the given value is not a number.”
JavaScript Functions
Definition
- Function
- A function is a block of JavaScript code defined once and invoked whenever its task needs to be done.
Functions keep code for a particular task in one place. This avoids repeating the same statements and makes programs clearer and easier to reuse. A function can have parameters or no parameters.
Two types of JavaScript functions
| Pre-defined functions | User-defined functions |
|---|---|
| Already provided in the JavaScript library. | Written by the programmer for a required task. |
| Also called Library functions. | Help divide a program into modules. |
Pre-defined JavaScript Functions
Pre-defined functions are already available in the JavaScript library. They perform common tasks, such as changing the case of a string, finding its length, or checking whether a value is a number.
Common pre-defined functions
- toUpperCase() converts a string into uppercase.
- toLowerCase() converts a string into lowercase.
- length finds the length of a string.
- parseInt() converts a float value into an integer.
- parseFloat() is listed as converting a given string into an integer.
- alert(), prompt(), write(), and isNaN() are also named as pre-defined functions.
Worked example
What do these function calls produce?
Try it first, then show the solutionSolution
- If x = "java", x.toUpperCase() changes the letters to uppercase.
- If x = "JAVA", x.toLowerCase() changes the letters to lowercase.
- If x = "JAVA", x.length finds the string length.
- parseInt(34.234) converts the float value to an integer.
- parseInt("34.23") converts the given string to an integer.
Answer: The listed results are JAVA, java, 4, 34, and 34.23, respectively.
Definition
- isNaN()
- isNaN() checks whether a value is not a number and returns true when it is not a number.
In the number-checking example, checknum() reads the value entered in a form. It uses isNaN(n) to decide which message to place in another form field: “Not a Number” or “It is Number”.
Creating and Using User-defined Functions
User-defined functions let programmers divide large programs into smaller parts called modules. A function definition can include parameters, which work as local variables inside the function. When the function is invoked, arguments provide values for its parameters.
function function-name(parameters list) { Declaration of variable; Executable statements; }
function-name is a valid identifier. The parameters list contains valid variable names, separated by commas when there is more than one. The function body is enclosed in braces.
Worked example
Define a function that adds two values and returns their sum.
Try it first, then show the solutionSolution
- Use sum as the function name and x and y as parameters.
- Add x and y and store the result in m.
- Return m to the place where the function was invoked.
Answer: function sum(x,y) { var m=x+y; return m; }
In the sum example, prompt() gets two inputs and parseInt() converts them to integers. The program calls sum(v1, v2), then displays the two values and their sum. A function can return a value that becomes the value of the function invocation expression.
Remember: Parameters belong to the function definition; arguments provide the values when the function is invoked.
Functions in Forms and an Online Quiz
A form can call a user-defined function when a button is clicked. In the number-checking example, checknum() checks the entered value. In the quiz example, checkAnswer() checks whether the first radio-button option is selected and displays a message using alert().
Definition
- getElementById()
- getElementById() returns the element whose ID attribute matches the specified value.
The quiz uses getElementById("myQuiz") to access the form. Its elements[0] refers to the first option, labelled MS-Word. If that option is checked, the program displays a correct-answer message; otherwise, it displays an incorrect-answer message.
Online quiz flow
- Show a question with radio-button options.
- Call checkAnswer() when the form is submitted.
- Check whether the first option is selected.
- Display the matching message with alert().
Questions students ask
Short, direct answers to what students search for in this chapter.
What is a function in JavaScript?
A function is a block of JavaScript code defined once and invoked whenever its task is needed. It keeps related statements together, can reduce repeated code, and helps make a program clearer and easier to reuse.
What is the difference between parameters and arguments in JavaScript?
Parameters are variable names written in a function definition. They work as local variables inside the function. Arguments are the values supplied when the function is invoked, and those values are used for the function’s parameters.
What are pre-defined functions in JavaScript?
Pre-defined functions are functions already available in the JavaScript library, so they are also called Library functions. Examples include toUpperCase(), toLowerCase(), parseInt(), alert(), prompt(), and isNaN().
What does isNaN() do in JavaScript?
isNaN() checks whether a given value or variable is not a number. It returns true when the value is not a number. A program can use its result to display a message about the entered value.
How does getElementById() work in the online quiz example?
getElementById() returns the element with the specified ID. The quiz uses it to access the form with ID myQuiz. Then elements[0] refers to the first option, labelled MS-Word, so the function can check whether it is selected.
Book back answers
10 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
Part-I Choose the correct answer
The parameters work as
- A) Local variable
- B) Global Variable
- C) File variable
- D) block variable
Try it, then show the answerAnswer
A) Local variable
Parameters work as local variables for the body of the function.
From the book: “parameters that work as local variables for the body of the function”
Predefined functions are also called as
- A)Library functions
- B) storage functions
- C) instructions
- D) commands
Try it, then show the answerAnswer
A)Library functions
Pre-defined functions are already defined in the JavaScript library and are also called Library functions.
From the book: “which are also called Library functions”
Larger programs are divided into smaller are called
- A) modules
- B) block
- C) sets
- D) Group
Try it, then show the answerAnswer
A) modules
The chapter says large programs are divided into small programs called modules.
From the book: “such large programs are divided into small programs are called modules”
Which of the following is used to enhance reusability and program clarity.
- A) functions
- B) modules
- C) sets
- D) instructions
Try it, then show the answerAnswer
A) functions
Functions keep code for a particular job in one place, enhancing reusability and program clarity.
From the book: “in one place in order to enhance reusability and program clarity”
Practice
What is one main benefit of placing code for a task in a function?
Exam questions
Practice questions with model answers. These are not from an official paper.
What is a JavaScript function, and why is it useful?2 marks
A function is a block of JavaScript code for a specific task that can be invoked repeatedly. It helps avoid repeated statements and improves reusability and program clarity.
Name the two types of JavaScript functions and explain pre-defined functions.2 marks
The two types are pre-defined or library functions and user-defined functions. Pre-defined functions are already defined in the JavaScript library; examples include isNaN(), alert() and toUpperCase().
Explain parameters and arguments in a function.2 marks
Parameters are identifiers in a function definition that work as local variables in the function body. Arguments are the values supplied to those parameters when the function is invoked.
How does the lesson's online quiz use a function to check an answer?2 marks
The checkAnswer() function checks whether the first option in the form is selected. If it is, an alert says the answer is correct; otherwise, an alert says it is incorrect and asks the user to try again.
Watch out
- Do not confuse parameters, which appear in the function definition, with arguments, which are values supplied when the function is invoked.
- Do not assume isNaN() returns true for a number; it returns true when the given value is not a number.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →