Why it's showing error? JS | SoloLearn: Learn to code for FREE!

+2

Why it's showing error? JS

https://code.sololearn.com/WvQXYEdB2NA2/?ref=app

7/2/2020 5:47:12 PM

Himanshu Rai

27 Answers

New Answer

+6

x *= 5 += 5 What the meaning of this..? What you are trying? It's a invalid syntax.. (5+=5 means 5 = 5 + 5. ?! Wrong.) So write only.. x*=5

+4

var x = 5 y= x*5 document.write(x+y)

+3

Himanshu Rai I already given explanation in previous post, may you need to refresh page.. x*=5+=5 is evaluated as x*=(5+=5) here 5+=5 evaluated as 5 = 5 +5 is his correct? 5 is constant not variable to store 5+5.. Is not it? Hope it clear now.. If x*= x+=5 evaluated as x*= (x+=5)

+3

x *= 5; // x = 25 x += 5; // x = 30 I am assuming 30 is the final value of x you are looking for. To do so using multiple assignment operators in one line move the code around a little bit replace the 5 with a x and boom Var x = 5; x += x *= 5; I document.write(x); The reason for your error is because var works from right to left. Your code in stages: 5 += 5; x *= 5; The first part gives you the error because 5 is not a var. My code in stages: x *= 5; x += x;

+3

Try this : let x=5; let y= x*5; y+=5; document.write (y);

+3

“probably , If this is what ya mean mate .? “ var x = 5; x *= 5 , x+= 5; document.write(x);

+3

x *=5 +=5 It's invalid assignment You can try x *=x +=5 but it's value will be 50 if you want to find value 30 you can try x *=x +=1 Now you find how it's working :> from right to left so we need a var where we assign

+2

Himanshu Rai in JavaScript, (in most languages) , empty string or obsence of value return false. Otherwise if it has some value otherthan empty, & for numbers if non-zero then it returns TRUE. (zero value means return false) So "xyz" as treated true in boolean, and "" empty string as false...

+2

"xyz" is a string with a value, not boolean type value. But it's boolean equalent value returns true but it is not itself return boolean value. To get equalent boolean value, use Boolean function like Boolean("xyz") ; now it return true Boolean("") ; this returns false.. But in your code "xyz" is a string type value but true is a boolean type value so not same both.. And also note this that in any expression evaluations, like a+b, str+a+true... there internally it will be first type casted to highest type then evaluated to higher type, if need returns to required type by type casting... So if int + double, then all converted to double first, if str + bool are converted to string types... So there "xyz" not equals to true... Edit: Himanshu Rai This link will far better clear your doubt I think, take a look.. https://www.geeksforgeeks.org/javascript-boolean/amp/?ref=rp

+2

You should separate assignament x*=5; X+=5; Then the result is going to be 30

+2

+1

We can use multiple assignment operators in one line. So how we can do so??

+1

Refer assignment operator lesson in js course where same example is given in yellow box with different values

+1

Bagon ok but why it is not working. Both the syntax (my own and your) are following same concept then why other is not working. Is there any specific rule for that?

+1

There used a variable but you used a constant 5. So error. It should be like x*=x+=5; Always right hand side operation fisrt evaluated then result is used for left hand operation if needed so x=x+5 is evaluated first then x=x*5 with updated x value... If x=5, then it results like x=x+5=5+5=10, Then x=x*5=10+5=50... If it clears..

+1

Jayakrishna🇮🇳 well thanks for answering, But still I'm confused. Is there any rule with that? I written x *= 5 += 5. (Let say x = 5) Means as left hand side operated first so first var x will be multiplied by 5 and the value 25 is assigned. Now value of x is 25. Then the next assignment Operator is operated which is +=. So 5 will be added to value 25 and resulting value should be assigned to var x. So now value is 30. So what's wrong with this?????

+1

Jayakrishna🇮🇳 i think you want to say that right hand side is first operated. Means let say we have code: x *= 5 += 6 So in this condition, 5 += 6 is first operated. So as we read, assignment Operator can only assign value to variable. So in this case, 5 is not variable, so this operation can not be done. Pls reply to is I'm right???

+1

Himanshu Rai yes..

+1

🧐🧐🧐🧐 Jayakrishna🇮🇳 It cleared now. Thanks👍

+1

Jayakrishna🇮🇳 one more things I want to ask you that in lesson data types. There said that Boolean value can true only in one condition, when we have written empty string. Like: x = "" So if I write code... x = "" If ( x == true) {document.write("value is true")} Else {document.write("value is false")} in above code, the value of x is empty string which is true in Boolean value. and in if statement, I have said that if x equals to true (in Boolean data type), output that value is true. So in above code, what will be result : true or false ???