Skip to content
the poor coder

10.3.1. Function Syntax

Poor Man
Published
Reading time
2 min read
On this page

Function Syntax in JavaScript#

If you're new to programming, you might feel intimidated by the syntax of functions in JavaScript. But don't worry, it's not as complicated as it seems!

Basic Function Structure#

A function in JavaScript is a block of code that performs a specific task. It can take inputs (also known as parameters) and produce outputs (also known as return values).

Here is the basic structure of a function:

Text

function functionName(parameter1, parameter2) {
  // function body
  return result;
}

The function name is followed by parentheses, which may or may not contain one or more parameters separated by commas. The function body, enclosed in curly braces, contains the code that executes when the function is called. The return statement, if included, specifies the value that the function will produce.

Function Invocation#

To use a function in your code, you need to call it. Here's how you can invoke a function:

Text

functionName(argument1, argument2);

The function name is followed by parentheses containing the arguments that you want to pass to the function. These arguments are assigned to the parameters declared in the function definition. The result of the function, if any, can be stored in a variable or used directly in your code.

Arrow Functions#

In ES6, a new syntax was introduced for creating functions called arrow functions. Arrow functions are a shorthand way of writing function expressions.

Here's how you can define an arrow function:

Text

const functionName = (parameter1, parameter2) => {
  // function body
  return result;
};

The arrow function expression consists of the const keyword, followed by the function name (if any), an equals sign, and parentheses containing the parameters. The function body is enclosed in curly braces, and the return statement, if included, specifies the value that the function will produce. Note that the arrow function does not use the function keyword.

Here's how you can invoke an arrow function:

Text

functionName(argument1, argument2);

Conclusion#

Functions are an essential part of JavaScript programming. By understanding the syntax of functions, you'll be able to write more complex and powerful code. Remember to practice writing functions on your own and experiment with different ways of defining and invoking them!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC