How to create a function Javascript
By Tan Lee Published on Dec 18, 2024 156
To create a function in JavaScript, you can use this simple syntax:
function functionName() { // function body }
Here's an example of a function that adds two numbers:
function add(a, b) { return a + b; } let result = add(2, 4); // Calling the function with 2 and 4 console.log(result); // This will print 6
In this example, the add
function takes two parameters, a
and b
, and returns their sum. When calling add(2, 4)
, it computes 2 + 4
, returns 6
, and stores the result in the result
variable, which is then printed to the console.
In JavaScript, the return
statement stops a function's execution and sends a value back to the caller. Once a return
is executed, the function exits immediately, and any code after it is not executed.
- How to use sweetalert2
- How to Pass string parameter in an onclick function
- How to format number with commas and decimal in Javascript
- What does 'use strict;' means in Javascript
- How to detect if caps lock is pressed in Javascript
- How to create a Custom Event in Javascript
- How to Check if an Object Has a Property Properly in JavaScript
- How to convert an Uint8Array to string in Javascript
Categories
Popular Posts
Portal HTML Bootstrap
Nov 13, 2024
Freedash bootstrap lite
Nov 13, 2024
Motiv MUI React Admin Dashboard Template
Nov 19, 2024
K-WD Tailwind CSS Admin Dashboard Template
Nov 17, 2024