javascript questions
Hey, i have a couple of questions.
1) What is the difference between changing a string to a number using
Number("5")
and parseInt("5")
. I know that with parseInt
it can separate a number from a string e.g. it can pull 5
out of something like parseInt("5Text")
. But in the original use case, is it a matter of personal preference or is there a correct practice? I assume if you don't want to pull a number from a string you'd just use Number()
?
2) What does the term "closures" mean? I've researched it but i don't really understand anything regarding it. I might have found a poor resource though.
Thanks in advance.7 Replies
1) i mean the biggest difference is in the name. one is parsing to Number and the other is parsing to Int. Number includes int (whole number) and floating point (deciman number) numbers. where's int just means whole number.
Number("10.9") // -> 10.9
parseInt("10.9") // -> 10
if i want to pull a number from a string, i would not use either. I'd just use regex
2) normally when a function ends the variables declared within that function goes out of that function scope (if u don't know what scopes are, check it out. otherwise u won't understand closure) abd thus are marked as eligible for garbage collection. in simple words say for example
i have declared a variable 'a'. when function ends js interpreter will mark the variable 'a' as eligible for garbage collection, and at some point the garbage collector will sweep and clear the data from memory that are marked just like 'a' hence maming it no longer accessible. cause it went out of scope and is cleared.
now clouser is when despite function ending, the internal variable is still accessible and is not marked as eligible for garbage collection. let's see an example.
here the out() returns another function with uses the variable a. because the useage of the variable a still persists, despite the function ending, the variable will not be marked at suitable for garbage collection.
despite being outside the out() function, as u can see we can still see the value of a cause it has not been cleared.
on the contrary, in the first case if we had done
this will throw error cause a is not accessible
this whole process is called a closure
the 2nd one ofc, not the 1st example
Ah I see, thatโs super clear now, thanks. So when youโre returning
a
in a function in your second example, does it have to be a function, or will return a
simply be enough so that it doesnโt get marked for garbage collection?if u do return a then it will just always return u the value of a , where is when u returned a function which called the console log on a, it needed to keep the a for future reference.
Oh I see, okay, thank you.
Oh and with this, this there no
parseFloat
type of thing? Is it simply just Number
and parseInt
?there is parseFloat as well yes
Oh okay, cool
be careful:
parseFloat
doesn't always give you the exact number you want, because of rounding errors
javascript uses ieee 754 for the floating numbers
by the way, 0.3 doesn't exist - it's 0.300000011920928955078125
https://www.h-schmidt.net/FloatConverter/IEEE754.html <-- you can use this to check the real number