Local and global variables in Javascript - 00013

deliaslan

Adem Deliaslan

Posted on October 6, 2019

Local and global variables in Javascript - 00013

please ignore xPos and yPos values
codesample from khan academy

var lifeInches = 20; //

var calcInches = function(startAge, endAge, inchesPerYear) {
var totalInches = inchesPerYear * (endAge - startAge);
lifeInches += totalInches;
return totalInches;
};

fill(255, 0, 0);

text("From 0-8 years:", 10, 20);
text(calcInches(0, 8, 2.5), 10, 40);

text("From 8-16 years:", 10, 100);
text(calcInches(8, 16, 2), 10, 120);

text("Total grown over life:", 10, 168);
text(lifeInches, 10, 200);

output:
"From 0-8 years: 20"
"From 8-16 years: 16"
"Total grown over life: 56"

lifeInches -> global variable

totalInches -> local variable

calcInches -> local variable

💖 💪 🙅 🚩
deliaslan
Adem Deliaslan

Posted on October 6, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Javascript Learning From Scratch -00003
javascript Javascript Learning From Scratch -00003

September 29, 2019

Arrays in Javascript - 00014
javascript Arrays in Javascript - 00014

October 23, 2019

Functions in Javascript - 00011
javascript Functions in Javascript - 00011

October 6, 2019

Text in Javascript -00010
javascript Text in Javascript -00010

October 6, 2019