+ 2

Why the code is being executed even the condition is not true?

https://code.sololearn.com/WeQKTq5wO457/?ref=app var batteryLevel = 10 var batteryLevel = 90 if (batteryLevel = 100){ alert ("Battery is full") }

26th Jul 2017, 3:14 PM
Waqas Shah
Waqas Shah - avatar
4 ответов
+ 4
because u have assigned 100 to battery value.. and it is not comparing it.. u need to use == operator
26th Jul 2017, 3:22 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Why is that not considered as a condition instead of assigning a new value ? @Sami Khan
26th Jul 2017, 3:25 PM
Waqas Shah
Waqas Shah - avatar
+ 1
because that was an assignment operator.. This is " ==" the comparison operator
26th Jul 2017, 3:42 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
@Sami Khan Thank you! Now I got it. The assignment operator will assign a value and won't evaluate or compare two things to make the condition. We need something that would make a condition not a decision :)
26th Jul 2017, 3:48 PM
Waqas Shah
Waqas Shah - avatar