Lesson 3
Functions
Reusable JavaScript code
1What is a Function?✨
A function in JavaScript is a block of code designed to perform a specific task. Functions help us organize our code and make it reusable. Instead of writing the same code multiple times, we can define a function once and call it whenever we need it!
2Why Learn Functions?🎯
- ✓ Helps make your code cleaner and easier to read
- ✓ Allows you to reuse code without rewriting it
- ✓ Makes debugging easier since you can test functions independently
- ✓ Essential for building complex applications
3Creating Your First Function💡
Let's create a simple function that greets a user. Here’s how you do it:
function greet(name) {
console.log("Hello, " + name + "!");
}greet("Alice"); // This will output: Hello, Alice!
4Function Parameters and Arguments🚀
Functions can take inputs, called parameters, which allow them to work with different values. You can pass arguments when calling a function:
function add(a, b) {
return a + b;
}console.log(add(5, 3)); // This will output: 85Returning Values🔍
Functions can return values using the return statement. This is useful when you want to use the result of a function in other calculations:
function square(num) {
return num * num;
}console.log(square(4)); // This will output: 166What You'll Build This Grade⭐
- ✓ Interactive web pages that respond to user actions
- ✓ Simple games using functions for different actions
- ✓ Calculators that perform various operations
- ✓ Fun projects like quizzes and surveys
🎯 Practice Challenges
Master Functions by completing all 3 difficulty levels
Functions - Easy Challenge
Start with the basics. Perfect for beginners!
Functions - Medium Challenge
Get more challenging. Build your confidence!
Functions - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Functions? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 2: Variables & TypesData types in JavaScript
- Next lesson →Lesson 4: DOM ManipulationChange web pages dynamically
- Grade 9 courseJavaScript Advanced - Web MasteryJavaScript coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 8 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.