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

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.

Related

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

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.

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

parseInt is not returning NaN when there is a letter [duplicate]

This question already has answers here:
How does parseInt() in Javascript work?
(4 answers)
Closed 3 years ago.
parseInt('5aab4') //5
// I expect the output is NaN.
My logic fails, due to parseInt. I assume that parseInt always returns NaN, when the input contains a letter?
No, parseInt() allows "garbage" (non-number) characters after some numeric characters. The parseFloat() function has the same "feature".
If you want to get a number, you can use Number("123") or simply the + unary operator. However those accept any JavaScript number, including ones with fractional parts or numbers in scientific notation. But those methods will fail and give NaN if the string input is not a valid numeric constant.
You could do something like
if (+someString === Math.floor(+someString))
I guess.
edit — a comment notes that you'd also want to check the degenerate case of an empty or all-space string too. A simple regular expression (/^\d+$/) followed by a sanity check that it's not 200 digits long (amonth possibly other things) is another alternative.
If you were wanting to check NaNanother way would be to test it with isNaN().
isNaN() will return a boolean giving you a verification for a given input.
parseInt('5aab4') //5
isNaN('5aab4') // true

Convert array of strings to array of numbers [duplicate]

This question already has answers here:
convert string array to integer array
(4 answers)
Closed 4 years ago.
I have a jQuery array ["3434", "3433"]
And I would like to make it like [3434, 3433]
No need for jQuery. Supposing you're sure they're all numbers (that is, we're not checking for errors), you can map them with Number, which converts them from string to numbers.
["3434", "3433"].map(Number);
Considering that Number returns NaN in case of error, you may then want to filter the result to remove undesired elements.
let nums = ["3434", "3433", "foo"].map(Number).filter(n => !isNaN(n));
console.log(nums);

java script string comparison [duplicate]

This question already has answers here:
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Closed 6 years ago.
I took a JS course on a website , and in one of the lessons there was a piece Of code that did not make sense to me :
the code is in the picture , why str1 is less then str2 ?
Strings are compared based on standard lexicographical ordering, using Unicode values. That means "a" < "b" and "c" > "b"
Two strings are strictly equal when they have the same sequence of
characters, same length, and same characters in corresponding
positions. source
var str1 = "aardvark";
var str2="beluga";
console.log(str1 < str2);//true
console.log(str1.length < str2.length);//false
This compares each character from 0-index, for example "a"<"b" thi is true. If there are equal, it compares next index, and next, ...
"aad">"aac", because, twice "a"="a" and then "d">"c"
JavaScript in this case will compare the strings lexographically character by character, where the letter 'a' is lower than the letter 'b' and so on. It works for numbers too, and the uppercase alphabet is considerd higher than the lowercase alphabet.
So, in your example, 'a' < 'b' and therefore the statement is true.

Categories