loading...
Cover image for Functions in JavaScript

Functions in JavaScript

bhagatparwinder profile image Parwinder 👨🏻‍💻 Updated on ・2 min read

JavaScript with Parwinder (36 Part Series)

1) What is Javascript? 2) Where can I run JavaScript? 3 ... 34 3) Types in JavaScript 4) What are objects in JavaScript? 5) Reference vs Value 6) Arrays in JavaScript 7) Array Methods in JS 8) Array Instance Methods 9) Array Instance Methods II 10) Functions in JavaScript 11) Type of Functions in JavaScript 12) Scope in JavaScript 13) Hoisting in JavaScript 14) Closures in JavaScript 15) Debugging in JavaScript: Console Methods 16) The new Keyword in JavaScript 17) JavaScript: Type Conversion 18) JavaScript: While Loop 19) JavaScript: Do While Loop 20) JavaScript: For Loop 21) Lesser-Known JavaScript Tricks 22) Events & Event Handling 23) Event Bubbling 24) Event Capturing 25) JS: Switch Statement Explained 26) Arrow Function: Basics 27) this Keyword in JavaScript 28) Arrow functions & this keyword 29) Callback Functions & Callback Hell 30) Strict Mode in JavaScript 31) Optional Chaining 32) Timers & Intervals 33) Arrow Function and The new, arguments & super Keyword! 34) Promises: Introduction 35) Promises: Chaining, Error Handling & Operators 36) New Promise Methods: allSettled & any

What are functions? What is their role in JavaScript?

A function is a JavaScript code block that can be executed by calling/invoking it. A function has the following essential parts:

  1. It is declared using the function keyword.
  2. The function name then follows it. This is the name that will be used to call the function.
  3. You can pass arguments to a function. These are values that could be dynamic.
  4. The arguments are assigned to parameters of the function and are encapsulated in round brackets ()
  5. This is followed by the code block that will be executed whenever the function is called.
  6. The code block is surrounded by curly braces {}

Function Image

A function is used to create reusable code blocks. If you a have piece of code that is to be executed multiple times, it is a good idea to convert it into a function.

Function Returns

A function can return you something once it has finished executing the code. This is not always the case. Sometimes functions execute the code and end.

Function with return:

function sum(a, b) {
    return a + b; // You return the value using return keyword
}

const x = sum(2,5);
const y = sum(7,9);
console.log(x); // 7
console.log(y); // 16

Function without return:

console.log("Hello World!");
// Hello World
// undefined

If you run the above example in your browser console, you would see that even though the statement "Hello World" is printed in the console, the function is not returning anything. That is why you would see your browser print out undefined after the statement.

JavaScript in-built functions

You don't always have to write your functions. JavaScript comes with a ton of functions that are ready to use.

console.log(parseInt()); // NaN (Not a number)
console.log(parseInt("234xyz")); // 234, parseInt ignore non integer
console.log(Math.random()); // 0.00746544513267
console.log(Date.now()); // 1590557812411

JavaScript with Parwinder (36 Part Series)

1) What is Javascript? 2) Where can I run JavaScript? 3 ... 34 3) Types in JavaScript 4) What are objects in JavaScript? 5) Reference vs Value 6) Arrays in JavaScript 7) Array Methods in JS 8) Array Instance Methods 9) Array Instance Methods II 10) Functions in JavaScript 11) Type of Functions in JavaScript 12) Scope in JavaScript 13) Hoisting in JavaScript 14) Closures in JavaScript 15) Debugging in JavaScript: Console Methods 16) The new Keyword in JavaScript 17) JavaScript: Type Conversion 18) JavaScript: While Loop 19) JavaScript: Do While Loop 20) JavaScript: For Loop 21) Lesser-Known JavaScript Tricks 22) Events & Event Handling 23) Event Bubbling 24) Event Capturing 25) JS: Switch Statement Explained 26) Arrow Function: Basics 27) this Keyword in JavaScript 28) Arrow functions & this keyword 29) Callback Functions & Callback Hell 30) Strict Mode in JavaScript 31) Optional Chaining 32) Timers & Intervals 33) Arrow Function and The new, arguments & super Keyword! 34) Promises: Introduction 35) Promises: Chaining, Error Handling & Operators 36) New Promise Methods: allSettled & any

Posted on by:

bhagatparwinder profile

Parwinder 👨🏻‍💻

@bhagatparwinder

Happy, cheerful, confident. Web developer based out of Chicago :)

Discussion

markdown guide