-3

Can anyone explain to me why this code doesn't works correctly:

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

Or all is a string or all is a Number.

I appreciate any help.

lc.
  • 113,939
  • 20
  • 158
  • 187
Muribury
  • 3
  • 4

1 Answers1

1
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
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • Thanks, i don't know javascript interpret or understand NaN like a number. – Muribury Aug 11 '15 at 09:19
  • It does seem like an oxymoron that "not a number" is a number !!! one of the many endearing qualities of javascript that does your head in at times! – Jaromanda X Aug 11 '15 at 09:20