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
TOMUTO ๐Ÿ…
TOMUTO ๐Ÿ…โ€ข4d ago
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
function f() {
let a = 10;
console.log(a);
}
function f() {
let a = 10;
console.log(a);
}
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.
function out() {
let a = 10;
return () => {console.log(a)};
}
function out() {
let a = 10;
return () => {console.log(a)};
}
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.
js
const x = out(); // returns a function and stores it in variable x
x(); // -> 10, prints a
js
const x = out(); // returns a function and stores it in variable x
x(); // -> 10, prints a
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
function f() {
let a = 10;
console.log(a);
}

console.log(a);
function f() {
let a = 10;
console.log(a);
}

console.log(a);
this will throw error cause a is not accessible this whole process is called a closure the 2nd one ofc, not the 1st example
snxxwyy
snxxwyyโ€ข3d ago
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?
TOMUTO ๐Ÿ…
TOMUTO ๐Ÿ…โ€ข3d ago
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.
snxxwyy
snxxwyyโ€ข3d ago
Oh I see, okay, thank you. Oh and with this, this there no parseFloat type of thing? Is it simply just Number and parseInt?
TOMUTO ๐Ÿ…
TOMUTO ๐Ÿ…โ€ข3d ago
there is parseFloat as well yes
snxxwyy
snxxwyyโ€ข3d ago
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
Want results from more Discord servers?
Add your server