Javascript condition evaluation: compare number literal [duplicate] - javascript

This question already has answers here:
Why is string "11" less than string "3"? [duplicate]
(6 answers)
Closed 3 years ago.
I was writing Javascript condition:
"70.5" > "129" evaluate to true, while "70.5" > "729" evaluate to false. What does this mean?
PS. In the end, I get the code working by parseFloat(70.5) > parseFloat(129). Want to know why I could not compare directly. Thanks.

What is happening is that string literals are being compared with one another. While you are writing the number 70.5, JS sees this as a string with characters '7', '0', '.', '5'.
String literals are compared by their ASCII codes. So, a character that has a bigger ASCII code will be "larger" than the character that has a lower ASCII code.
Similarly,
var a = "a" > "b";
document.write(a); // Gives false
the above code snipped would print false, while
var a = "c" > "b";
document.write(a); // Gives true
would return true.
See this for more info.

Related

Compare two strings in JS to see if the first big string contains exact same match or not [duplicate]

This question already has answers here:
How do I find an exact word in a string?
(5 answers)
Closed 28 days ago.
I want to compare the following two variables to see if they match exactly or not.
What I am doing right now is:
var string1 = "S1";
var string2 = "LS1 B26 M90";
let result = string2.indexOf(string1);
It returns 1 which means S1 exists in string2. I want it to look for "S1" and not to match with "LS1".
you can simply achive by below:
String("LS1 B26 M90").split(" ").includes("LS1")
String("LS1 B26 M90").split(" "): convert string into string list.
.includes("LS1"): will check the existence it return true in case of
match otherwise false.

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

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.

Javascript: regular expression and .test [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 6 years ago.
I'm new to JS - this might be easy for you guys to answer. I've been reading on regular expression but couldn't figure out the full meaning of this code:
I've been asked to write a myParseInt method with the following rules:
It should make the conversion if the given string only contains a
single integer value (and eventually spaces - including tabs, line
feeds... - at both ends).
For all other strings (including the ones representing float values),
it should return NaN.
It should assume that all numbers are not signed and written in base
10.
The answer is:
function myParseInt ( str ) { return /^\s*\d+\s*$/ . test (str) ? + str : NaN; }
(please correct me if I'm wrong!) But I sort of understand the first and last part (/^\s* and \s*$) where it matches the beginning and end of str input with white space character. The \d+ part matches digit characters 1 or more times.
The .test(str) part matches the str with the stated regular expressions and gives it a true or false value -
but why is there ? after .test(str), then + str: NaN;? I am unsure what does the ? do, the : syntax. I know it has something to do with if it doesn't match, gives NaN. But I am trying to get clarifications regarding the syntax which I couldn't find on MDN.
Many thanks!
The ? is a ternary operation.
You write a condition that returns true/false followed by a question mark, then what to return for truthy and falsy.
It is taken from C.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

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