Declaration & Expression Flashcards
What is Function Declaration ?
Function that can be used before it’s declared
Example :-
function calAge(birthyear) { return 2050 - birthyear; }
const age1 = calAge(1984) console.log(age1);
What is a Expression ?
Essentially a function value stored in a variable
Example :-
const calAge2 = function (birthyear) { return 2037 - birthyear }
const age2 = calAge2(1991);
console.log( age2)
Coding Challenge -
1. The world population is 7900 million people. Create a function declaration called ‘percentageOfWorld1’ which receives a ‘population’ value, and returns the percentage of the world population that the given population
represents.
For example, China has 1441 million people, so it’s about 18.2% of the world population
2. To calculate the percentage, divide the given ‘population’ value by 7900 and then multiply by 100
- Call ‘percentageOfWorld1’ for 3 populations of countries of your choice, store the results into variables, and log them to the console.
- Create a function expression which does the exact same thing, called ‘percentageOfWorld2’, and also call it with 3 country populations (can be the same populations)
function percentageOfWorld1(population) { return (population / 7900) * 100; } const percentageOfWorld2 = function (population) { return (population / 7900) * 100; }; const percPortugal1 = percentageOfWorld1(10); const percChina1 = percentageOfWorld1(1441); const percUSA1 = percentageOfWorld1(332); console.log(percPortugal1, percChina1, percUSA1);
OUTPUT :-
0.12658227848101267 18.240506329113924 4.2025316455696204