๐— ๐—ผ๐—ฟ๐—ฒ ๐—”๐—ฏ๐—ผ๐˜‚๐˜ ๐—๐—ฆ ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€

Functions stop code duplication.

If you add numbers repeatedly without functions, you write the same logic over and over. This makes code hard to read and hard to fix.

Using a function allows you to write the logic once and use it many times.

Functions help with:

Naming Rules Do not use spaces in names. Use camelCase. The first word starts with lowercase. Every other word starts with uppercase.

Good names:

Bad names:

Parameters vs Arguments This is a common interview topic.

Parameters are variables in the function definition. They receive values. Example: function add(a, b)

Arguments are the actual values you pass when you call the function. They supply values. Example: add(10, 20)

Default Parameters You can set a fallback value. Example: function greet(name = "Guest")

If you call greet() without a name, it uses "Guest".

The Return Statement Use return when you need to use the result elsewhere. Use return to store a value in a variable. Use return to send data to another function.

If you do not use return, your function returns undefined.

Important Rules:

Hoisting JavaScript moves function declarations to the top of your code before execution. This means you can call a function before you write it in your file.

Passing Values JavaScript passes values, not the variables themselves. If you change a parameter inside a function, the original variable outside stays the same.

Source: https://dev.to/annapoo/more-about-function-in-js-5b19