Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren+ 1
How could it be 11? If you help me please?
Why? As we know value in the in the inverted comma that means 'String' so that we can print the exact number what we get it in the variable as it's value? var a = '1'; var b = '1'; console.log(a+b %7);
11 Antworten
+ 11
JS op precedence says % (remainder) takes precedence over + (addition). You evaluate the statement by considering b%7 first. '1' gets type casted to integer, and 1%7 results in 1. a + 1 is string concatenation, not addition, which results in '11'.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
+ 5
Addition to Hatsy Rei explanation, '1' gets typecasted(converted) to integer because:
""Many built-in operations that expect numbers first coerce their arguments to numbers ~ mdn web docs""
See this:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus
+ 4
Hatsy Rei best answer 👌
+ 2
Yes yes
+ 2
Hey marn.
Cast your variables to number() before using them, you logic is using string concatenation not math operations.
console.log((Number(a)+Number(b))%7)
the above should give you expected results
+ 2
Sony Hossain
The program execution is also the same in this case, only the values will be different. try to put the new values in the steps above.
..and don't worry you are new to javascript. Javascript tries to do some things automatically which leads to confusion.(even experienced js developers make mistakes)
For now, you need to learn "typecasting" and "operator precedence" in javascript. If mdn links are difficult to understand, try 'w3schools.com' website.
links :-
Javascript Types: https://www.w3schools.com/js/js_type_conversion.asp
Operator Precedence: https://www.w3schools.com/js/js_precedence.asp
+ 1
Thank you all but still looking for the best solution it's too hard to catch up the point ..
+ 1
The variables "a" and "b" are both strings containing the character "1".
The "+" operator is used to concatenate the strings, resulting in the string "11".
The "%" operator is then applied to the string "11", which attempts to convert the string to a number before performing the modulo operation. Since the string cannot be converted to a number, it is treated as NaN (Not a Number), which has a numeric value of NaN. Therefore, the expression "b % 7" evaluates to NaN.
The NaN value is then concatenated to the string "11", resulting in the final string "11NaN".
The console.log() function outputs the string "11NaN" to the console.
+ 1
Sony Hossain
var a = '1';
var b = '1';
console.log(a+b %7);
> a + b % 7
> '1' + '1' % 7
// remainder has higher precendence, therefore it will be evaluated first. (adding brackets to make it easier)
> '1' + ('1' % 7)
// Javascript tries to convert string to integer to perform the operation. why? (see the link in previous reply)
> '1' + (1 % 7)
> '1' + 1
// when you try to add number to a string, js converts the number to string and then perform the addition
> '1' + '1'
> '11'
and that's why output is '11'
+ 1
Sandeep thank you so much bro!
0
Thank you so much for your reply.. dear friend what about the following code if I try to change the values then.. ?
See this... I just got mad.. to make output with the remainder operator (%).
var a = '7';
var b = '9';
console.log(a + b % 7);
// output 72