Declaring a variable in Javascipt

I need help why I did not have to declare/define the variable in this javascript code but yet it still worked? Does anyone have any insight?

https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscj-22-javascript-syntax-part-i/modules/fscj-22-practice-javascript-syntax-variables-data-types-conditionals-functions/lessons/javascript-fundamentals-code-challenge/exercises/num-imaginary-friends

function numImaginaryFriends(tF) {
return Math.ceil(tF)  ;
} 

console.log(numImaginaryFriends(6.9)) 
1 Like

Truth be known, you did declare it, in the formal parameter. tF exists in the local namespace (local scope) as though it was declared as follows:

var tF = arguments[0]  //  a property of Function objects

We can do anything with that variable inside the function, but not outside, where it cannot be seen.

const foo = function (formal_parameter) {
    return formal_parameter
}
console.log(foo('argument'))
// argument

To see how the earlier code might look,

const bar = function () {
    return Array.from(arguments).slice(0, arguments.length)
}
console.log(bar(1,2,3,4,5))
// [1, 2, 3, 4, 5]

This is a property that arrow functions do not have, albeit they still have the spread operator for the same general convenience. That’s one major difference to remember. More study on this topic is recommended. Not now, though. Bookmark this page for future reference a little ways down the road.

1 Like