java script string comparison [duplicate] - javascript

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.

Related

Javascript Regex plus operator usage [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
What is the meaning of + in a regex?
(5 answers)
Closed 6 months ago.
So I've read a lot of posts and documentation on the + operator for JS regex, but I'm still unsure of what it wouldo do.
For example, one of the websites I looked after gave the following example:
const nums = /\d+/
console.log(nums.test('456'))
Says it would look for infinite possibilites for d. But then...
const nums = /\d/
console.log(nums.test('456'))
... has the same result.
So I tried using numbers... for instance:
const nums2 = /45/
const nums 3 = /45+/
console.log(....
But testing both regex would STILL give the same result. If I put any numbers before or after 45 with or without the + it will still give me a "true" if 45 is togheter.
So can someone explain what the + symbol in regex means and it's usage in a way I can understand?
There's no difference in the cases you tried -- if there's one digit, then there's also one or more digit, so they both match.
But if you use it together with other patterns you can have a difference.
console.log(/A\dB/.test("A123B"));
console.log(/A\d+B/.test("A123B"));
The first one is false because it only matches a single digit between A and B; the second is true because it matches any number of digits.
The difference can also be useful if you use .match() instead of .test(). This returns the part of the string that matched the regexp, and \d+ will return the entire number, while \d will just return the first digit.

Javascript condition evaluation: compare number literal [duplicate]

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.

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.

javascript comparison of strings

I have the following script
document.write("12" < "2");
which returns true. Any reason why? The documentation says that javascript compares strings numerically but, I don't see how "12" is less than "2".
JavaScript compares strings character by character until one of the characters is different.
1 is less than 2 so it stops comparing after the first character.
I believe it is doing a lexicographic comparison - the first char in string one is '1', which is less than the first char of string two, which is '2'. More about Lexicographic order here: http://en.wikipedia.org/wiki/Lexicographical_order
This is because the first character of "12" is 1, which comes before "2"; and JavaScript string comparison is lexically/alphabetically, rather than numerically. Though it appears partially numeric, since 1 is sorted ahead of 2.
You can, however, simply compare the numbers as numbers:
document.write(parseFloat("12") < parseFloat("2"));
Try:
document.write(parseInt("12") < parseInt("2"));

Categories