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)
Related
This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
What's the best way to convert a number to a string in JavaScript?
(25 answers)
Closed 2 years ago.
I know that this question was asked about in PHP but I could not find anything in javascript.
I have a random number generated between 000(yeah it is just three zero's but it is shown that way from how the number is generated) and 999999999 and I want to test true or false for if whether it includes a specific sequence of numbers like 777, 9000, 28, or the like of any length and from any beginning of numbers...
for example, finding 289 in 678342891 or 728987699 would be true, and finding 289 in 678529187 or 023829564 would be false.
is this possible, and how would I do it?
you can use .includes method in JS after transforming both the number and the other number to strings using .toSting method
let n = 12345589;
let sub = 55;
let sub2 = 25;
function isSeq(number, sub){
number = number.toString(10);
sub = sub.toString(10);
return number.includes(sub);
}
console.log(isSeq(n, sub));
console.log(isSeq(n, sub2));
Check whether your test number is a substring in your long random number. Since you have mentioned that 000 is a value, this tells me that it is a string already(no need to call toString() for it)
var random=678342891;
var search1=289;
var search2=777;
console.log( random.toString().indexOf(search1.toString()) != -1 );//true
console.log( random.toString().indexOf(search2.toString()) != -1 );//false
A function to test it would look like so:
function test(random,search){
return random.toString().indexOf(search.toString()) != -1
}
This question already has answers here:
Is true == 1 and false == 0 in JavaScript?
(10 answers)
What exactly is Type Coercion in Javascript?
(9 answers)
Closed 3 years ago.
I am trying to compare true+false to 1 i.e true+false===1
In the past, I tried the following:
true===1 //false
false===0 //false
true-true===0 //true
I expect the output to be false but the actual output is true.
true and false are implicitly type casted to number when used in mathematical operation
Note:- here + is type casting it to number
console.log(+true)
console.log(+false)
console.log(true+false)
so
true + false === 1
is actually
1 + 0 === 1
When you add up two Booleans, since the Booleans do not support arithmetic operation, but logic operation only, the JS environment just converts them to Number. So 1+0===1. But if you do true ===1, this is type sensitive, it will result false. If you do true == 1, it’ll be true. Cuz this is not type sensitive, type transformation will be done by JS environment.
This question already has answers here:
In javascript, is an empty string always false as a boolean?
(6 answers)
Closed 4 years ago.
'' == false // true
' ' == false // true
if ('') {
// this code is not run
}
if (' ') {
// this code is run
}
As you can see, there are interesting results.
The empty string is treated as falsy, as we already know.
However, whitespace strings are treated as truthy or falsy, depending on the situation.
You can see the link posted in your question about toBoolean behavior. Another interesting tidbit;javascript has another comparison operator === that checks for value and type
'' == false // true
'' === false // false
A blank String is false.
console.log(' '==true);
console.log(''==true);
This is often the problem with the == operator; it attempts to coerce the types of the objects it is comparing to be equal. If you use the === operator, it will not attempt type coercion.
console.log(''===false);
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;
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.