Why the small number string is greater than big number string [duplicate] - javascript

This question already has answers here:
Javascript compare numbers as strings
(2 answers)
Closed 2 years ago.
When the smaller number string when compared with the larger number string - the result is true.
How is it possible?
document.write(`Why the result of '2'>'10' is ${'2'>'10'}`)
Any expert here?

You're comparing strings, so a lexical comparison is performed instead of a numerical comparison.
Lexically, 2 comes after 1.

Related

JavaScript: Why would "1" - "1" return 0? [duplicate]

This question already has answers here:
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Closed 3 years ago.
I was trying to subtract the last digit out of a string after a loop, but then I found this mysterious phenomenon.
When I add two string of numbers, they concatenate:
"1" + "1" // = "11"
But when I subtract a string of number from another, it did not decatenate but was casted as a number instead:
"11" - "1" // = 10
Why does this happen?
Should the result of the subtraction be "1" instead of 10?
Wouldn't having some kind of consistency be better?
Edit: This question is NOT a duplicate of the question below, as this question is asking about the subtraction of two strings, instead of a string with a number.
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
The subtraction operator (-) subtracts the number to the right of the
operator from the number on the left.
When either of the operands are strings, an attempt is made to convert
the strings to numbers.
Source

How to convert a long String of numbers into number type in Javascript [duplicate]

This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 4 years ago.
I am converting a long Sting of numbers values to number data type with Number() and parseInt() method of js but it is returning a hexadecimal value.
Does anybody know how to convert a long string values of number to Number data type?
// "1245458787546574878756756754561464567867"(type:String) to 1245458787546574878756756754561464567867 (type:Number)
var str = "1245458787546574878756756754561464567867";
console.log(Number(str)); //1.2454587875465749e+39
console.log(parseInt(str)); //1.2454587875465749e+39
console.log(parseInt(str,10)); //1.2454587875465749e+39
console.log(str.length); //40

JS number function adds up zeros at the end [duplicate]

This question already has answers here:
JavaScript summing large integers
(10 answers)
Closed 5 years ago.
I am using the Number() JS function which is supposed to convert string value to numeric.
It's working fine for small numbers. For big ones - it is starting to substitude values with zeros as shown on the image:
Is there a work around for this problem?
In JS, the largest integral value is 9007199254740991 That is, all the positive and negative integers should not exceed the -9007199254740991 and 9007199254740991 respectively.
The same is defined as the 253-1.
console.log(Number.isSafeInteger(parseInt('1111111111')))
console.log(parseInt('1111111111'))
console.log(Number.isSafeInteger(parseInt('111111111111111111')))
console.log(parseInt('111111111111111111'))
//9007199254740991 - The largest JS Number
console.log(Number.isSafeInteger(parseInt('9007199254740991')))
This is because you're using numbers that are larger than Number.MAX_SAFE_INTEGER and Javascript does not guarantee to represent these numbers correctly
Use Number.isSafeInteger to check:
> Number.isSafeInteger(Number('111111111111111111'))
< false

Is it possible to have a number / float in JavaScript with two decimal values as zeros? [duplicate]

This question already has answers here:
Trailing zeros in javascript
(2 answers)
JavaScript: Decimal Values
(5 answers)
Can JSON.stringify output a whole number formatted as a double?
(2 answers)
Closed 6 years ago.
I would like to return the number, 15.00, in a json payload: {"theNumber" : 15.00}. All my research on SO has suggested to do something like parseFloat(15).toFixed(2), but this always returns a string and therefore does not solve the problem of returning 15.00 as a number / float.
Is it possible to return a number in JS with two decimal values as zeros?

What is the cause? operators or use of strings? [duplicate]

This question already has answers here:
Why is string "11" less than string "3"? [duplicate]
(6 answers)
Closed 3 years ago.
Why does this happen with strings in javascript?
3<=255
true
but
'3'<='255'
false
Is it something to do with the operators or the use of strings?
I guess it is because it compare ascii values of chars and 3 had greater ascii value than 2. In string it compare char by char if 1 char is false it wont compare else
In first case you are comparing 2 Numbers, in second you are comparing 2 strings. So they are different types and thus produces different results.
Both.
When the comparison is done on numbers, the values of the numbers determine the outcome.
When the comparison is done on strings, the sort order of the strings determine the outcome.
The string '255' is considered smaller than the string '3', because it would come before it in a sorted list.

Categories