Code Monkey home page Code Monkey logo

flw1-u2l2-23-24-student-exercises's Introduction

Lesson 2.2: Arrow Functions & Review

Table of Contents

  1. Arrow Functions
  2. Single Parameter
  3. Multiple Parameters

Arrow Functions

What is a function?

In programming, a function is a reusable block of code that performs a specific task. Functions help organize your code, make it more readable, and reduce redundancy.

Why do we use functions in programming?

  • Modularity: Functions allow you to break down complex problems into smaller, manageable tasks.
  • Reusability: You can use the same function in multiple parts of your code.
  • Readability: Functions make your code more organized and easier to understand.

Quick Recap

Arrow functions provide a concise syntax to write functions in JavaScript. They are declared with the following structure:

const functionName = () => {
  // Your code here
}

Here's an example:

const greet = () => {
  console.log('Hello!');
}

You can often convert a regular function into an arrow function. For example:

// Regular function
function greet() {
  console.log('Hello!');
}

// Arrow function
const greet = () => {
  console.log('Hello!');
}

Code Along

Use the arrow.js file to complete the tasks. As you code, make sure you are following along with the examples provided in the lesson.

Single Parameter

In this section, you'll learn about functions that take a single parameter.

Function Declaration with Parameters

To declare a function with a parameter, use the following syntax:

function functionName(parameterName) {
  // Your code here
}

Here's an example:

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

Function Call with Arguments

When calling a function with parameters, you provide arguments. Arguments are the actual values that you want to use inside the function. For example:

squareNum(7); // Calling squareNum with the argument 7
squareNum(6); // Calling squareNum with the argument 6

let userInput = input.value; // Assuming input.value holds a number
squareNum(userInput); // Calling squareNum with a variable as the argument

Multiple Parameters

Functions can also take multiple parameters.

function greet(name1, name2) {
  console.log(`Hi ${name1} & ${name2}!`);
}

Multiple Parameters - Order Matters

When using multiple parameters, it's important to place the arguments in the right order when you call the function. The order of the arguments should match the order of the parameters in the function declaration.

greet("Lucy", "Pablo"); // Correct order

Happy coding ๐Ÿ˜Š

flw1-u2l2-23-24-student-exercises's People

Contributors

cn-curriculum avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.