arrow function - one line and no parameters

Arrow Function - One Line and No Parameters

Arrow functions in JavaScript provide a concise and more readable way to write functions. They are commonly used in functional programming and are especially helpful when working with arrays and objects.

In this case, we want to create an arrow function that has no parameters and only one line of code. This can be done in the following way:


const funcName = () => console.log('Hello World!'); 

In the above code, we declare a constant variable called funcName and assign it an arrow function. The parentheses indicate that there are no parameters, and the code after the arrow is the function body. In this case, we're simply logging a string to the console.

Another way to write this function would be to use the return keyword:


const funcName = () => {
  return 'Hello World!';
};

This code is equivalent to the previous one. The only difference is that we're using braces to define the function body and explicitly returning a value.

One important thing to note when working with arrow functions is that they do not have their own this keyword. Instead, they inherit it from the enclosing lexical scope. This can sometimes lead to unexpected behavior if you're not careful.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe