parseInt() in Javascript giving weird results [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: why does parseInt(1/0, 19) return 18?
Why does parseInt(1/0, 19) evaluate to 18 in Javascript ? I understand 19 in not a permissible radix but still can someone tell how things are working here ?

Ah, quick javascript consoling led to the answer:
> 1/0
Infinity
> parseInt("Infinity", 19)
18
parseInt seems to convert the first argument to a string, e.g.:
> parseInt(11, 2)
3
so, it's converting the string "Infinity", which explains everything.

Related

Javascript: null==0 returns false, but null<1 returns true. Please explain [duplicate]

This question already has answers here:
Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?
(2 answers)
Closed 2 years ago.
very weird behaviour indeed.
Please explain the implicit conversions that happen in each case.
null will be converted to the number 0 (because < works with numbers), and 0 is less than 1.

console.log weird behaviour with numbers [duplicate]

This question already has answers here:
Console.log output in javascript
(6 answers)
Closed 3 years ago.
console.log(01) results in 1
But
console.log(011) results in 9
Can someone explain how console.log works with such numbers?
It's not about console.log, a number that starts with 0 is octal notation
console.log(+"011") // if you use like this it will work
011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.

Converting from number to string gives strange number in Javascript [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?
Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal

JavaScript less than or equals (bool <= int) [duplicate]

This question already has an answer here:
How do the JavaScript relational comparison operators coerce types?
(1 answer)
Closed 8 years ago.
I am wondering if anyone can explain the behavious of comparison operators when using booleans and integers.
Why do the following statements produce the results they do?
false < = 9 // true
false >= 9 // false
Thanks
The representation of false in memory is 0. That's why you obtain those results.

How come parseInt("08") = 0, parseInt("07") = 7 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function parseInt() doesn't parse numbers with leading 0 correctly
Strange issues when parsing in JS occur.
parseInt("08")
//The result is: 0
parseInt("07")
//The result is: 7
Why is this happening?
Because of the 0 prefix. It tells Javascript the number is Octal, in base-8. 8 isn't a legal octal digit.
Use parseInt("8") instead, or as #Gumbo so correctly pointed out - parseInt("08", 10)

Categories

Resources