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

Understanding the difference between let and var

Before ECMA 6, the common way of creating a variable was with the use of var. However, using var sometimes introduces bugs that mostly show up at runtime and others that are not revealed at runtime but may affect the way your code works.

Some of the properties of var that introduce bugs as mentioned in the previous paragraph are as follows:

  • var allows the redeclaration of variables.
  • var is not blocked scope; hence, it is either attached to the global scope or to a function scope.

Let's discuss the two properties listed above in detail.

var allows the redeclaration of variables

var gives users access to redeclare variables along the line, hence overriding the previous variable of the same name. This feature might not show an error if not caught, but will certainly affect the behavior of the code:

var population_count = 490; 
var new_count = 10; 
 
//along the line; you mistakenly re-declare the variable 
var population_count = "490"
 
//do some arithmetic operation with the variable 
var total_count = population_count + new_count 
 
//output: "49010" 

In the preceding code snippet, there won't be any error, but the main objective of the code is altered just because var did not alert us that such a variable has been declared already.

Let's say we replace var with let, as shown in the following code:

let population_count = 490;
// ...some other code goes here 
let population_count = "490"
 
//output: Error: Identifier population count as already being declared 

You can see from the preceding error output that let, unlike var, will not allow you to declare a variable in the same namespace twice.

Next, let's look at the scope property of variables declared with var.

var is not a blocked scope

Variables declared with var have the following properties:

  • They are readily available in the scope to which they are defined.
  • They are available to scope within the range they are being declared.

In the following code, we will check how the estimate variable declared with var is accessible across all the scope within the variable declaration scope:

var estimate = 6000;
function calculate_estimate() {
  console.log(estimate);
}
calculate_estimate() // output 6000
 
if(true){
 console.log(estimate);
}

Now, for a blocked scope such as if, while loop, and for loop, the code within the blocked scope is meant to be run when the scope is available. Likewise, the variable is meant to exist only when the scope is available, and once the scope is not available again, the variable should not be accessible.

Declaring variables with var makes the preceding statement not possible. In the following code, we declare a variable using var and investigate its availability across all possible scopes:

if(true){
 var estimate = 6000;
}
console.log(estimate)

This will output the estimate as 6000. The variable is not meant to exist outside the if block. Using let helps to solve this:

if(true){
 let estimate = 6000;
}
console.log(estimate)
//output: ReferenceError: estimate is not defined

This shows that using let to declare variables helps reduce unprecedented bugs in your code. In the next section, we'll discuss another important concept called destructuring.

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