JavaScript for Beginners – Chapter 7: Functions

Another chapter in the JavaScript for beginners’ series! In this chapter we cover functions, what they are used for and how to write them.

Functions

A function is a way to encapsulate a portion of your code so a specific action can be reuse…


This content originally appeared on DEV Community and was authored by Danish Saleem

Another chapter in the JavaScript for beginners’ series! In this chapter we cover functions, what they are used for and how to write them.

Functions

A function is a way to encapsulate a portion of your code so a specific action can be reused and called on to run at a specific time.

Declaration

An example of what the syntax to declare a function looks like.

function showMessage() {
  alert("Hello World!");
}
showMessage();

Naming

Since functions are actions their name is usually a verb. Keep the names brief but accurate to describe what the function does.

showMessage(); // Shows a message

getAge(); // Returns the age

calcSum(); // Calculates the sum and returns the result

createForm(); // Creates and returns a form

checkPermission(); // Check a permission, returns true/false

Arguments

A function can require you to pass some arguments (agrs) which act as variable within the function. This allows for better reusability.

function calcAge(currentYear, birthYear) {
  return currentYear - birthYear;
}

const myAge = calcAge(2021, 1989); // Output: 32

const johnAge = calcAge(2021, 1975); // Output: 46

Local Variables

A variable declared inside a function will only be visible within the function scope.

function showMessage() {
  let message = "Hello World!"; // Local variable

  alert(message);
}

showMessage(); // Output: Hello World!

alert(message); // Output: Error! - Variable is local to the function

Return a Value

A function can return a value - usually one generated or manipulated by the function code.

const movieAgeLimit = 13;

function checkAge(age) {
  if (age >= movieAgeLimit) {
    return true;
  } else {
    return false;
  }
  // Alternative: return age >= movieAgeLimit
}

const hasAccess = checkAge(18);

if (hasAccess) {
  alert("Enjoy the movie");
} else {
  alert("You are not old enough for this movie");
}

Call another Function

It can also be used to perform an action or call another function

const yourName = prompt("What is your name?");

function showWelcomeMessage(name) {
  alert(`Welcome ${name}`);
}

showWelcomeMessage(yourName);

Summary

  • Function names tend to be verbs to easily describe what the function does.
  • Arguments are values you can pass to be used within a function.
  • Local Variables declared within a function are only visible within the function scope.
  • A function can return a value or call another function / action.
  • Use functions to store reusable code that can be called as and when needed.

Let's connect 💜

You can follow me on Twitter, Instagram & GitHub

If you like this post. Kindly support me by Buying Me a Coffee

Buy Me a Coffee


This content originally appeared on DEV Community and was authored by Danish Saleem


Print Share Comment Cite Upload Translate Updates
APA

Danish Saleem | Sciencx (2021-12-11T08:28:28+00:00) JavaScript for Beginners – Chapter 7: Functions. Retrieved from https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/

MLA
" » JavaScript for Beginners – Chapter 7: Functions." Danish Saleem | Sciencx - Saturday December 11, 2021, https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/
HARVARD
Danish Saleem | Sciencx Saturday December 11, 2021 » JavaScript for Beginners – Chapter 7: Functions., viewed ,<https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/>
VANCOUVER
Danish Saleem | Sciencx - » JavaScript for Beginners – Chapter 7: Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/
CHICAGO
" » JavaScript for Beginners – Chapter 7: Functions." Danish Saleem | Sciencx - Accessed . https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/
IEEE
" » JavaScript for Beginners – Chapter 7: Functions." Danish Saleem | Sciencx [Online]. Available: https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/. [Accessed: ]
rf:citation
» JavaScript for Beginners – Chapter 7: Functions | Danish Saleem | Sciencx | https://www.scien.cx/2021/12/11/javascript-for-beginners-chapter-7-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.