I'm making a calculator in javascript and I'm wondering which code is better for storing variables.
The following is some code of a function that operates when an operator is clicked.
In this case, oldNum is the first value before the operator, and newNum is the second value after the operator.
code 1:
result ? (oldNum = result) : newNum ? (oldNum = newNum) : (oldNum = oldNum);
code 2 :
result ? (oldNum = result) : !newNum || (oldNum = newNum);
CodePudding user response:
Both codes are syntactically valid.
I would prefer code with fewer conditions if possible for readability and to avoid confusion. If both codes yield the same results, then having more conditions is not necessary unless you have an objective and use for them.