Javascript 转换为数字不';行不通
有人能给我解释一下为什么这个代码不能正常工作吗Javascript 转换为数字不';行不通,javascript,numbers,parseint,Javascript,Numbers,Parseint,有人能给我解释一下为什么这个代码不能正常工作吗 var num = '10'; Number(num); console.log(typeof(num));//string parseInt(num); console.log(typeof(num));//string parseFloat(num, 10); console.log(typeof(num));//string console.log('-------------'); var num = '10'; var stri
var num = '10';
Number(num);
console.log(typeof(num));//string
parseInt(num);
console.log(typeof(num));//string
parseFloat(num, 10);
console.log(typeof(num));//string
console.log('-------------');
var num = '10';
var string = 'aklñjg';
num = Number(num);
string = Number(string);
console.log(typeof(num));//number
console.log(typeof(string));//number
num = parseInt(num);
string = parseInt(string);
console.log(typeof(num));//number
console.log(typeof(string));//number
console.log('++++++++++++++++');
var num = '10';
var string = 'aklñjg';
num = Number(num);
string = Number(string);
console.log(typeof(num));//number
console.log(typeof(string));//number
num = parseInt(num, 10);
string = parseInt(string, 10);
console.log(typeof(num));//number
console.log(typeof(string));//number
或者all是字符串,或者all是数字
非常感谢您的帮助。您说的工作不正常是什么意思?谢谢,我不懂javascript,也不把NaN理解为一个数字。“不是一个数字”就是一个数字,这似乎是一个矛盾修饰法!!!javascript的许多可爱特性之一,有时会让你陷入沉思!
var num = '10'; // num is a string
Number(num); // you've done nothing with the RESULT, num is unchanged
console.log(typeof(num));//string - because you haven't changed num
parseInt(num); // you've done nothing with the RESULT, num is unchanged
console.log(typeof(num));//string - because you haven't changed num
parseFloat(num, 10); // you've done nothing with the RESULT, num is unchanged
console.log(typeof(num));//string - because you haven't changed num
var num = '10'; // num is a string
var string = 'aklñjg'; string is a string
num = Number(num); // num is a Number
string = Number(string);// string is a Number (NaN (not a number) is a number!)
console.log(typeof(num));//number
console.log(typeof(string));//number
num = parseInt(num); // num is a number
string = parseInt(string); // string is a number (NaN still a number)
console.log(typeof(num));//number
console.log(typeof(string));//number