Content ITV PRO
This is Itvedant Content department
The Backbone of Modular codes-1
Learning Outcome
5
Recognize how functions improve code reusability and structure
4
Call functions and interpret returned results
3
Pass values to functions using parameters
2
Write and define simple JavaScript functions
1
Understand what control statements are in JavaScript
We can avoid this by creating reusable functions
Reusable blocks of code that perform specific tasks
What is Function?
Encapsulate Code
Promote Reusability
Simplify Maintenance
Enable Abstraction
Improve Readability
1
2
3
4
5
Need for Functions
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("student");
The function keyword is used to declare functions
After the `function` keyword, we declare the name of the function
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("student");
Inside the function, we passed a parameter
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("student");
We called the parameter here
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("student");
Inside this function block `{ }`, we write the code to be executed
const greet = function(name) {
return `Hello, ${name}!`;
}
console.log(greet("Students"));
Function Expression
CODE:
OUTPUT:
Summary
5
Functions are a core building block of JavaScript programs
4
Return statements send values back to the caller
3
Parameters allow data to be passed into functions
2
They reduce repetition and improve readability
1
Functions group reusable code into blocks
Quiz
What is a function in JavaScript?
A. A variable that stores numbers
B. A data type
C. A block of reusable code
D. A loop
Quiz-Answer
A. A variable that stores numbers
B. A data type
C. A block of reusable code
D. A loop
What is a function in JavaScript?
By Content ITV