Learn Functions in JavaScript | Day 34 | Arrow And Return Keyword | Web development course 2023
rrow functions are a concise way to write JavaScript functions. They were introduced in ES6 and offer several advantages over traditional function declarations, including:
Conciseness: They are written using shorter syntax.
Lexical this binding: They capture the this value of the enclosing scope.
Implicit return: They can implicitly return the value of the expression in their body.
Here's the basic syntax of an arrow function:
JavaScript
(parameters) => expression
Use code with caution. Learn more
The parameters and the expression are separated by an arrow (=>). If the function has no parameters, you can use empty parentheses.
Return Keyword:
The return keyword is used to explicitly return a value from a function. However, in arrow functions with a single expression in their body, the return keyword is implicit. This means you don't need to write it explicitly, and the expression's value will be the function's return value.
Here are some examples:
1. Single expression:
JavaScript
const double = (number) => number * 2;
const result = double(5); // result will be 10
Use code with caution. Learn more
2. Multiple expressions:
JavaScript
const isEven = (number) => {
if (number % 2 === 0) {
return true;
} else {
return false;
}
};
const number = 8;
const isEvenResult = isEven(number); // isEvenResult will be true
Use code with caution. Learn more
3. Implicit return:
JavaScript
const square = number => number ** 2;
const area = square(10); // area will be 100
Use code with caution. Learn more
Key Points:
Arrow functions are concise and have lexical this binding.
In arrow functions with a single expression, the return keyword is implicit.
You can still use the return keyword explicitly in arrow functions with multiple expressions.
-
2:26:29
Laura Loomer
11 hours agoEP108: Dems Embrace Domestic Terrorism To "Get Trump"
67.1K36 -
3:01:51
Right Side Broadcasting Network
14 hours agoWATCH: NASA’s SpaceX Crew-10 Launch
135K45 -
2:06:17
Glenn Greenwald
13 hours agoJudge Orders Hearing on Columbia Student Deportation Case; Is the Ukraine Ceasefire Plan Serious? Trump Attacks Thomas Massie for His Budget Vote | SYSTEM UPDATE #422
135K190 -
47:16
BonginoReport
15 hours agoTrump-Elon Bromance Triggers The Libs (Ep.03) - 03/12/2025
171K298 -
4:03:41
Barry Cunningham
16 hours agoTRUMP DAILY BRIEFING: PRESIDENT TRUMP PRESS CONFERENCE | DEMOCRATS IN PANIC!
111K98 -
1:56:30
Melonie Mac
14 hours agoGo Boom Live Ep 40!
64.8K11 -
1:58:30
Kim Iversen
14 hours agoDOGE, Trump, Aliens and Remote Viewing: A Deep Dive into the Unknown
78.8K83 -
1:20:08
Redacted News
15 hours agoPutin smells a TRAP as Ukraine agrees to Trump's U.S. ceasefire plan | Redacted w Clayton Morris
187K189 -
51:59
Candace Show Podcast
16 hours agoMahmoud Khalil’s Detainment: Fighting Terrorism Or Speech? | Candace Ep 158
138K496 -
7:43:31
Dr Disrespect
20 hours ago🔴LIVE - DR DISRESPECT - PUBG - PRO TACTICALLY WINNING
180K21