Sololearn: Learn to Code
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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
14th Feb 2023, 1:42 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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
14th Feb 2023, 2:00 PM
Sandeep
Sandeep - avatar
+ 4
Hatsy Rei best answer 👌
14th Feb 2023, 1:59 PM
D_Stark
D_Stark - avatar
+ 2
Yes yes
14th Feb 2023, 1:42 PM
Abdrabbo Alhmly
Abdrabbo Alhmly - avatar
+ 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
14th Feb 2023, 2:24 PM
Mawande Hobo
+ 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
15th Feb 2023, 12:26 PM
Sandeep
Sandeep - avatar
+ 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.
15th Feb 2023, 6:39 AM
Frances Balgos
Frances Balgos - avatar
+ 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'
15th Feb 2023, 8:22 AM
Sandeep
Sandeep - avatar