Javascript course — Reusing predefined calculation or task

Marcel Mokos
Nov 30, 2018 · 2 min read

Part #3: Functions — the block of code you can call with different input

Photo by Vitor Santos on Unsplash

It would be useful to write a block of code that will take input, perform computation, and returns output.

Similarly, as with variables, you can write a statement that will declare a function. A function is a “subprogram” that can be called. Functions are one of the fundamental building blocks in JavaScript.

Function declaration

Defining the function simply names the function and specifies what to do when the function is called.

A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:

  • The name of the function.
  • The JavaScript statements that define the function, enclosed in curly brackets{}.
A simple function will that just return “Hello world”

An input to function with parameters

A list of parameters can be provided to the function, enclosed in parentheses and separated by commas.

Declare function with parameters and calling it with arguments

A function can specify default input parameters

It will allow us to call a function with missing arguments.

Declaring function with default parameters allows us to call the function with missing arguments

A function can be assigned to a variable

Alternative more modern syntax using an arrow functions

Arrow functions are alternatively called lambda expressions.

Arrow function

What is confusing for beginners about arrow functions?

Arrow function can optionally use shorter syntax. Short syntax can be confusing for beginners.

As a beginner use parentheses (), curly bracket {}and specify return.

ableneo Technology

Application Development, Data Science, Experience Platforms

Marcel Mokos

Written by

I'm fanatic to next generation Javascript lambda, yield, async/await everything. I admire typescript and flow types. Javascript will ultimately rule the world.

ableneo Technology

Application Development, Data Science, Experience Platforms

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade