How do I write a function in JavaScript?
How do I write a function in JavaScript?
To write a function in JavaScript, you can use the following syntax:
1function functionName(parameters) { 2 // code to be executed 3 return something; // optional 4}
For example, a simple function that adds two numbers would look like this:
1function addNumbers(x, y) { 2 return x + y; 3}
Functions in JavaScript allow you to group code to be executed, making your code more organized and easier to understand. Happy coding!