-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
A function is a set of statements that performs a task or returns a value. Functions help you write modular, DRY (Don't Repeat Yourself) code.
function functionName(parameters) {
// code to execute
}
functionName
is the name you give to the function.
parameters
are input values passed to the function.
return
is used to send back a result.
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
function sayHi() {
console.log("Hi there!");
}
sayHi(); // Output: Hi there!
const add = function(a, b) {
return a + b;
};
console.log(add(2, 3)); // Output: 5
const multiply = (x, y) => x * y;
console.log(multiply(4, 5)); // Output: 20
function greet(name = "Guest") {
return "Welcome, " + name;
}
console.log(greet()); // Output: Welcome, Guest
return
sends a value back to the caller.
console.log()
just prints the value in the console.
Q1. How do you define a named function in JavaScript?
Q2. What is the difference between a function declaration and a function expression?
Q3. How can you write a function that takes two numbers and returns their sum?
Q4. What is the purpose of the return
statement in a function?
Q5. Write a function that takes a string and returns it in uppercase.
Q6. What are arrow functions, and how do they differ from regular functions?
Q7. How do default parameters work in JavaScript functions?
Q8. Can a function be stored in a variable? Show with an example.
Q9. What happens if a function is called with fewer arguments than parameters?
Q10. Write a function that calculates the factorial of a number using a loop.
Q1: Which keyword is used to define a function in JavaScript?
Q2: How can you call a function named myFunc?
Q3: What does a function return if there’s no return statement?
Q4: Which of the following is a function expression?
Q5: What are parameters in a function?
Q6: Which is a valid arrow function syntax?
Q7: What is the purpose of the return keyword in a function?
Q8: Which statement is true about functions in JavaScript?