;

Learning Functions in Basic Javascript

Introduction Function in Javascript Basics


Example Functions in Basic Javascript

buayaberdiri.blogspot.com - Functions are a fundamental concept in JavaScript, as they allow you to encapsulate and reuse code in a modular way. In this article, we'll cover the basics of functions in JavaScript, including how to define, call, and use them in your code.

Defining a function


A function in JavaScript is defined using the function keyword, followed by the function name, a list of parameters (optional), and the function body enclosed in curly braces. Here's an example:


function greet(name) {
  console.log(`Hello, ${name}!`);
}



In this example, we've defined a function called greet that takes a single parameter called name. The function body simply logs a message to the console using template literals.

Calling a function


Once a function is defined, you can call it by using its name followed by parentheses, passing in any arguments if required. For example, to call the greet function we defined earlier, we can do:


greet('John'); // Output: Hello, John!

This will output the message "Hello, John!" to the console.

Returning a value


Functions in JavaScript can also return a value using the return keyword. Here's an example:


function square(num) {
  return num * num;
}


In this example, we've defined a function called square that takes a single parameter called num. The function body returns the square of the parameter by multiplying it by itself.

To use the square function, we can call it and store the result in a variable:


const result = square(5);
console.log(result); // Output: 25


This will output the value 25 to the console.

Introduction About Arrow functions in Javascript


In addition to the traditional function syntax shown above, JavaScript also has a shorthand syntax called arrow functions. Arrow functions are a more concise way to define functions that take a single expression as their body. Here's an example:


const square = num => num * num;


This defines an arrow function called square that takes a single parameter called num. The function body returns the square of the parameter by multiplying it by itself.

To use the square function, we can call it and store the result in a variable:


const result = square(5);
console.log(result); // Output: 25


This will output the value 25 to the console.

Higher-order functions


In JavaScript, functions can be passed as arguments to other functions and returned as values from functions. Functions that take other functions as arguments or return functions as values are called higher-order functions.

Here's an example of a higher-order function that takes a function as its argument and calls it multiple times:


function repeat(func, num) {
  for (let i = 0; i < num; i++) {
    func();
  }
}


In this example, we've defined a function called repeat that takes two parameters: a function called func and a number called num. The function body uses a for loop to call the func function num times.

To use the repeat function, we can define a function to pass as the func argument:


function sayHello() {
  console.log('Hello!');
}

repeat(sayHello, 3); // Output: Hello! Hello! Hello!


This will output the message "Hello!" to the console three times.

Conclusion


Functions are a powerful feature of JavaScript that allow you to write reusable code in a modular way. By defining functions, you can encapsulate functionality and call it from other parts



See the article about javascript here:

  1. Learn Array Method - forEach, map, reduce, filter
  2. Learn Basics Array In Javascript
  3. Learn Basics Asynchronous In Javascript
  4. How To Change String Value Using Replace in Javascript
  5. How to Learn a Calculator Basic in Javascript
  6. What Are Switch in Javascript And How to Use Them
  7. Create a Calculator Using the Python Flask Framework
  8. Basic Learning to Make a Calculator With PHP and HTML
  9. How to Use a FOR Loop in Javascript
  10. Introduction Objects Basics in Javascript
  11. Learn IF Else and Else IF in Basic Javascript



Check out other articles about HTML & CSS:

  1. Learn Input Types In HTML For Login And Register Form
  2. Learn HTML List - How To Create Square, Circle, Number List
  3. How To Create Comments Line In HTML
  4. How to Add Text Formatting In HTML
  5. Adding The Fieldset Code To The Form In HTML
  6. How to Create a Search Box in Pure HTML
  7. Create Color Charts With Pure HTML
  8. How to change font size using CSS
  9. Types of Border Styles in CSS Code
  10. How to Change the Background Color in CSS




List of Article Posts https://buayaberdiri.blogspot.com