a=["s21aSi"]
gPf = function(a) {
var c;
a.forEach(function(b) {
c = b
})
gPg(c);
console.log('Inside gPf '+c)
}
gPg = function(z) {
console.log('Inside gPg', +z)
}
gPf(a)
Why does z
show as Nan
when I do console.log
inside gPg
. It's a string inside gPf
but show Nan
when it's inside gPg
delete '+' before z:
gPg = function(z) {
console.log('Inside gPg', z)
}
or
gPg = function(z) {
console.log(`Inside gPg ${z}`)
}
Because you are changing the signal of a Not-A-Number
, when you add before a number a signal(+ or -) you a force change the signal of this number, when you add a signal before a string, the JS force a convertion in this string to number, but the string isn't a number, so JS convert this to NaN
, see here to more details.
So to your code to work, you need remove +
before a variable z
, like this:
a = ["s21aSi"] gPf = function(a) { var c; a.forEach(function(b) { c = b }) gPg(c); console.log('Inside gPf:', c) } gPg = function(z) { console.log('Inside gPg:', z) } gPf(a)
'Inside gPf '+c
the plus sign (+) is being used to concatenate 2 strings.
'Inside gPg', +z
the plus sign (+) is used as a math (addition) function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.