Giter Site home page Giter Site logo

fictional-octo-fortnight's Introduction

Functions, ES6 and Scope

Lesson Objectives

To Understand...

  • Functions
  • ES6 Arrow Functions
  • Scope

Intros

Functions

In JavaScript, functions are objects that can be invoked to perform a specific task. They can be declared using the function keyword, and can take zero or more parameters as input. Functions can also return a value using the return keyword. Functions in JavaScript are often used for code reuse.

Functions Example

// define a function called "greet"
function greet(name) {
  console.log("Hello, " + name + "!")
}


// "call" the "greet" function 
greet("Alice")
greet("Bob")
Functions Example Output
Hello, Alice!
Hello, Bob!

ES6 Arrow Functions

ES6 introduced arrow functions, which are a shorthand way of writing functions in JavaScript. Arrow functions are anonymous functions that use the => syntax to define the function body. They also have a shorter syntax and an implicit return, which can make them more concise than traditional functions.

Arrow Function Example

const multiply = (x, y) => {
  return x * y;
}

console.log(multiply(2, 3));
console.log(multiply(4, 5));

Arrow Function Example Output

6
20

Scope

Scope refers to the accessibility of variables in different parts of your code. There are two types of scope in JavaScript: global scope and local scope. Variables declared outside of any function have global scope and can be accessed from anywhere in your code. Variables declared inside a function have local scope and can only be accessed within that function.

Scope Example

let globalVar = "I'm a global variable";

function myFunction() {
  let localVar = "I'm a local variable";
  console.log(localVar);
  console.log(globalVar);
}

myFunction();
console.log(globalVar);
Scope Example Output
I'm a local variable
I'm a global variable
I'm a global variable

Resources

W3 Schools

MDN Docs

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.