JavaScript use of variables basic functions -
JavaScript use of variables basic functions -
i'm learning javascript on codecademy. on there, gives illustration of basic variable:
var dividebythree = function (number) { var val = number / 3; console.log(val); }; however, seems me if variable within function unnecessary. not possible instead?:
var dividebythree = function (number) { console.log(number/3); }; it seems cutting out whole line of code. function still work , if so, why not used instead?
it looks purpose of code demonstrate 1 way how/where variables used. yes, replace variable value. there times when variable necessary, imagine instructing tool that.
one reason variables may assigned in situations used give them name. in case value poor illustration well, there scenarios may doing complex math or computing calculate value, , assigning name helps maintain readability.
another reason "cache" result (especially useful if you're doing dom lookups), not need re-compute value each time need it.
javascript
Comments
Post a Comment