when you compare 2 strings what are you comparing exactly [duplicate] - javascript

This question already has answers here:
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Closed 7 years ago.
with javascript when you compare 2 strings what are you comparing exactly
return "hello" > "hola"
this will return false, why ?

The strings are compared character-by-character (h vs h), then (e vs o) until either one (or both) of the strings ends, or you get an inequality. In this case 'e' is less than 'o'.
Every character is represented as a number.

The comparison in javascript strings first compares characters one by one from the beginning. An string is bigger than another when the same index characters of the first different character in both strings is bigger than another.
"hello" > "hola"
"h" == "h"
"e" != "o" --> "e" < "o"
this is why the result is false;

Related

Convert Form input from upper to lower cases [duplicate]

This question already has answers here:
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Converting php string to Title Case
(3 answers)
Closed 4 months ago.
I would need a Custom JavaScript for a WordPress form, converting upper cases to lower cases. I found some entries here in the community, but not the one I need.
What should be converted ?
For example I get a form entry 'PETER' and I want that the first digit 'P' stays in upper cases and the rest should be converted to lower cases. From 'PETER' to 'Peter'
Who can give a helping hand, please ? Thanks
You can get the sting first char with str[0], the other parts sliced with -1 argument.
const name = "PETER";
function PETERToPeter(str) {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
const nameChanged = PETERToPeter(name);
console.log(nameChanged);

Why isn't 2+3 == "2+3"? [duplicate]

This question already has answers here:
Add string to a number in Javascript
(2 answers)
Closed 2 years ago.
Consider the following:
var comp = 2+3;
console.log(comp == "5"); // true
console.log(comp === "5"); // false
console.log(comp == "2+3"); // false
Why isn't the 3rd comparison true? JS can convert a string to integer only if it's a single number? What happens in the 3rd comparison? Is it unable to convert "2+3" into a number so it just assumes it's different?
comp, which is 5 is a number type in JS and "5" is a string. A string and a number are not the same and thus console.log(comp === "5") will return false.
When you use == instead of === it returns true, because of a concept called "type coercion". This just means that JS assumes that you are trying to compare two of the same types—in this case numbers—because they seem to be identical, and returns true. So it converted the string to a number to help you out.
In the third case there are other types of characters in the string, in addition to numbers, and none of them are similar to the number 5, so type coercion does not apply and so it returns false.
Read more about type coercion here.
Because "2+3" with quotation marks signals javascript that this is an string and it cant be coerced to an number.
And 5 is not the same like "2+3".
Just test it: console.log(typeof '2+3') and console.log(typeof 2 + 3)

Weird add many Char in javascript [duplicate]

This question already has answers here:
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
(8 answers)
Closed 3 years ago.
Today i saw an example of JavaScript and i can not understand it.
('b' + 'a' + + 'a' + 'a').toLowerCase()
result: "banana"
I don't understand it, why JavaScript add 2 'n' between each 'a'?
The first ‘b’ and ‘a’ are simply strings being added as ‘ba’. After the second ‘a’, you see a double plus sign(+), the first one is for concatenation like the previous plus sign. But the second plus sign is called the unary operator which simply transforms the string into a number, if it isn’t already. Since ‘a’ cannot be converted to a number it is converted to ‘NaN’. The final ‘a’ is added to this ‘baNaN’ string and the final ‘baNaNa’ string is made. And to finish it up, the toLowerCase function is used and the output ‘banana’ is received.

Why does javascript Number ending with a dot returns a number but when within a regex function it fails [duplicate]

This question already has answers here:
Javascript casts floating point numbers to integers without cause
(2 answers)
How to validate digits (including floats) in javascript
(10 answers)
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
Any whole number ending in a dot returns the number in javascript console.(except decimal numbers)
like > 1. returns 1. Adding >1+1. also works. I don't understand why
typeof(1) // 'number'
typeof(1.) //'number'
However, when I put the same number inside a function, regex test gives a wrong output.
i.e,
const regex = /^\d+$/ //checks if there is a number inside a string
regex.test('1') // true
regex.test(1) //true
regex.test('1.') // false
The workaround I have is simply regex.test(Number('1.'))
JavaScript has a single type for all numbers: it treats all of them as floating-point numbers. However, the dot is not displayed if there are no digits after the decimal point:
5.000 = 5
Also, \d matches a digit, not a number.

Different Javascript behavior on substraction and sum [duplicate]

This question already has answers here:
Javascript addition and subtraction with a string value
(8 answers)
Closed 8 years ago.
Why such thing would happen in Javascript?
'5'+3 = 53
'5'-3 = 2
This is happening because + operator is overloaded. If any operand is a string, string concatenation is performed. If you have two numbers, addition is performed.
In other words
2+3=5
while '2'+3='23' and 2+'3'='23'.
On the other hand, for the - operator, it is not overloaded in such a way and all operands are converted to numbers.
'8'-2=6
because - is not overloaded and operand '8' will be converted to 8. Hence, we get 6.
For further information on this, please have a look here and read the paragraphs 11.6.1 and 11.6.2.
String concatenation is done with + so Javascript will convert the first numeric 5 to a string and concatenate "5" and "3" making "53".
You cannot perform subtraction on strings, so Javascript converts the second numeric i.e. "3" to a number and subtracts 3 from 5, resulting in "2" as the result.

Categories

Resources