Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Data-Driven Applications with Danfo.js

You're reading from   Building Data-Driven Applications with Danfo.js A practical guide to data analysis and machine learning using JavaScript

Arrow left icon
Product type Paperback
Published in Sep 2021
Publisher Packt
ISBN-13 9781801070850
Length 476 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Stephen Oni Stephen Oni
Author Profile Icon Stephen Oni
Stephen Oni
Rising Odegua Rising Odegua
Author Profile Icon Rising Odegua
Rising Odegua
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Section 1: The Basics
2. Chapter 1: An Overview of Modern JavaScript FREE CHAPTER 3. Section 2: Data Analysis and Manipulation with Danfo.js and Dnotebook
4. Chapter 2: Dnotebook - An Interactive Computing Environment for JavaScript 5. Chapter 3: Getting Started with Danfo.js 6. Chapter 4: Data Analysis, Wrangling, and Transformation 7. Chapter 5: Data Visualization with Plotly.js 8. Chapter 6: Data Visualization with Danfo.js 9. Chapter 7: Data Aggregation and Group Operations 10. Section 3: Building Data-Driven Applications
11. Chapter 8: Creating a No-Code Data Analysis/Handling System 12. Chapter 9: Basics of Machine Learning 13. Chapter 10: Introduction to TensorFlow.js 14. Chapter 11: Building a Recommendation System with Danfo.js and TensorFlow.js 15. Chapter 12: Building a Twitter Analysis Dashboard 16. Chapter 13: Appendix: Essential JavaScript Concepts 17. Other Books You May Enjoy

Arrow functions

Arrow functions are just unnamed or anonymous functions. The general syntax of arrow functions is shown in the following expression:

( args ) => { // function body }

Arrow functions provide a means of creating concise callable functions. By this, we mean arrow functions are not constructible, that is, they can't be instantiated with the new keyword.

The following are different ways of how and when to use arrow functions:

  • The arrow function can be assigned to a variable:
    const unnamed = (x) => {
    console.log(x)
    }
    unnamed(10) //  10
  • Arrow functions can be used as an IIFE (Immediately Invoked Function Expression). IIFEs are functions that once encountered by the JavaScript compiler are called immediately:
    ((x) => { 
        console.log(x) 
    })("unnamed function as IIFE") // output: unnamed function as IIFE
  • Arrow functions can be used as callbacks:
    function processed(arg, callback) {
        let x = arg * 2;
        return callback(x);
    }
    processed(2, (x) => {
        console.log(x + 2)
    });   // output:  6

While arrow functions are great in some situations, there is a downside to using them. For example, arrow functions do not have their own this scope, hence its scope is always bound to the general scope, thereby changing our whole idea of function invocation.

In the Understanding the this property section, we talked about how functions are bounded to their invocation scope and using this ability to support closure, but using the arrow function denies us this feature by default:

const Obj = {
     name: "just an object",
     func: function(){
          console.log(this.name);
     }
}
Obj.func() // just an object

Even though in the object, as shown in the code snippet, we make use of the anonymous function (but not the arrow function), we have access to the object's Obj properties:

const Obj = {
     name: "just an object",
     func:  () => {
          console.log(this.name);
     }
}
Obj.func() // undefined

The arrow function used makes the Obj.func output undefined. Let's see how it works if we have a variable called name in the global scope:

let name = "in the global scope"
const Obj = {
     name: "just an object",
     func:  () => {
          console.log(this.name);
     }
}
 
Obj.func() // in the global 

As we can see, Obj.func makes a call to the variable in the global scope. Hence, we must know when and where to use the arrow functions.

In the next section, we will talk about Promises and async/await concepts. This will give us the power to easily manage long-running tasks and avoid callback hell (callbacks having callbacks).

You have been reading a chapter from
Building Data-Driven Applications with Danfo.js
Published in: Sep 2021
Publisher: Packt
ISBN-13: 9781801070850
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime