DEV Community

Jenuel Oras Ganawed
Jenuel Oras Ganawed

Posted on

5

JavaScript 4 Ways To Create Function

First we have the Function Declaration. This is mostly common way to create a function as shown in the bellow example. What good about this is you can use the function even if the function is declared on the very bottom of your codes.

function addTwoNumbers(num1, num2) {
    return num1 + num2;
}
console.log(addTwoNumbers(1,10));
// outputs: 11
Enter fullscreen mode Exit fullscreen mode

Function Expression is a function were you asign a function in a variable. Function assigned to a variable needs to be declared on top before using the function.

console.log(addTwoNumbers(1,10)); // Error, becayse cant find addTwo Numbers
const addTwoNumbers = function (num1, num2) {
    return num1 + num2;
}
console.log(addTwoNumbers(1,10));
// outputs: 11
Enter fullscreen mode Exit fullscreen mode

Arrow Function Expression, this function is like a functional expression but instead of writing function we use arrows => instead.

const addTwoNumbers = (num1, num2) => {
    return num1 + num2;
}
console.log(addTwoNumbers(1,10));
// outputs: 11
Enter fullscreen mode Exit fullscreen mode

Concised Arrow Function Expression, is a function were you can directly return without writing a return statement. note: only works if It will directly return a value.

const addTwoNumbers = (num1, num2) => num1 + num2;
console.log(addTwoNumbers(1,10));
// outputs: 11
Enter fullscreen mode Exit fullscreen mode

Thanks for Reading my short read, If you like to Buy me coffee, click the image.

drawing

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more