JavaScript Functions & Spread Syntax

function  <name> (<param 1>,<param 2>...) {
body of the function
}

Default parameters

function for adding two numbers
function with default parameter

Arguments

Now imagine a situation that you don’t know how may parameters will be passed to your function. Like, you a teacher to 50 students and every year number of students will be increased. Now just image every time you have to add new parameters to make your function work. What a tough life!!

function with arguments

Rest parameters

Unilke, arguments there is another parameter which is called rest paramenter. It can take all your parameters and keep it as a array instance. You can perform array functions like forEach ,sort, map ,push or pop for rest params.

function with rest parameter

Function scope

For scope, you can think it like a zone. Think you want to share any secret about your love life and you have 3 best friends whom you trust the most.So this is your comfortable zone or you access scope, where you can keep you secrets.

sum declarled inside add function will get undefined error if used outide

Arrow Functions

shortcut way to write a function. Here, you won’t need to put function keyword or fuction name,. No need to declare variables or return statement as well.

(parameter) => expression

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store