DEV Community

Haryniramesh
Haryniramesh

Posted on

JAVASCRIPT

Why use functions?

Reusability: Write code once and use it multiple times.
Modularity: Break down complex problems into smaller, manageable pieces.
Readability: Makes your code easier to understand and maintain.
Avoid Repetition: Prevents writing the same code over and over.
Enter fullscreen mode Exit fullscreen mode

Ways to Define Functions in JavaScript

There are several ways to define functions in JavaScript, each with its own nuances:

Function Declarations 
Function Expressions 
Arrow Functions 
Immediately Invoked Function Expressions 
Enter fullscreen mode Exit fullscreen mode

1. Arrow Function
const add = (a, b) => a + b;

2. Anonymous Function

const greet = function(name) {
return "Hello, " + name;
};

๐Ÿงช Example: Add Two Numbers

function add(a, b) {
return a + b;
}

Usage:

console.log(add(5, 3)); // Output: 8

โŒจ๏ธ Form and Input Handling

const form = document.forms[0];

input.value

const name = document.getElementById("username").value;

Top comments (0)