CodeNewbie Community

Cover image for Basics of Functions in JS
Sankalp Swami
Sankalp Swami

Posted on

Basics of Functions in JS

Here we will be going through the very basics of javascript.
functions are one of the core fundamental building blocks in javascript.
Functions are like programs in your programs.It is a block of code designed to perform a particular task. Javascript function can be executed when you invoke or call it any time you want same as Hermione gives a wave to her wand anytime she wants
hermione

SYNTAX

function wingardiumLeviosa (parameter1, parameter2) {
  // code to be executed
  // make objects fly
}
wingardiumLeviosa(argument1, argument2)
Enter fullscreen mode Exit fullscreen mode

JS Function is defined with function keyword followed by its name and parantheses (). Parantheses may contain parameters. Inside the curly braces, the code to be executed is placed. The function is executed by invoking by its name with parantheses and arguments if any.

  • Parameters - Parameters are nothing but named variables passed into a function.
  • Arguments - The arguments are the values passed into a function.
  • Code - Code to be executed in the function placed inside the curly braces.
  • Return - Return statement ends the function and returns control to the calling function.It returns value to the calling function.

Lets write a function fullName which takes firstName and lastName as arguments and returns full name of a person.

function fullName(firstName, lastName) {
  let fullName = `${firstName} ${lastName}`;
  return fullName;
}
Enter fullscreen mode Exit fullscreen mode

Invoking or Calling a Function

You cannot execute a function by just defining it. For executing a function you need to invoke (call) a function.
You can call a function by just its name with parantheses.

fullName(arg1, arg2)
Enter fullscreen mode Exit fullscreen mode

The return value is stored in the calling function.
You can log the value to the console by

let a = fullName('Sankalp', 'Swami');
console.log(a); // will print: Sankalp Swami
Enter fullscreen mode Exit fullscreen mode

Some examples -

let a = fullName('Harry', 'Potter');
let b = fullName('Hermione', 'Granger');
let c = fullName('Ronald', 'Weasley');
let d = fullName('Lord', 'Voldemort');
console.log(a); // will print: Harry Potter
console.log(b); // will print: Hermione Granger
console.log(c); // will print: Ronald Weasley
console.log(d); // will print: You know who // just kidding
Enter fullscreen mode Exit fullscreen mode

Argument Defaults

When you have declared an parameter but nothing is passed as an argument you can set an parameter to default.

function fullName(firstName='-', lastName='-') {
  let fullName = `${firstName} ${lastName}`;
  return fullName;
}
Enter fullscreen mode Exit fullscreen mode

In the above example if one of the argument is not passed, it gets default value of -.

let a = fullName('Dobby');
console.log(a); // will print: Dobby -
Enter fullscreen mode Exit fullscreen mode

Function Scope

Functions create a new local scope. This contains variables defined in the function body as well as the arguments passed in the functions.

function add (num1, num2) {
  let result = num1 + num2;
  return result;
};
console.log(add(2, 4));
console.log(num1); // ReferenceError: result is not defined
Enter fullscreen mode Exit fullscreen mode

Here function add is defined at Global scope but variable num1 is passed as an argument inside the add function so, it is not accesible outside the function.

Once a function is defined, it can be used over and over and over again. You can invoke the same function many times in your program, which saves lot of your work.

🙌SUPPORT

Buy Me A Coffee

Thanks for reading, keep learning, Peace and Bubbyyee
Bubbyee

Discussion (0)