4

I was reading the Three.js code when a silly question arose: Is there any difference between the codes below?

frameHeight = frameHeight !== undefined ? frameHeight : 24;

and

frameHeight = frameHeight || 24;

(frameHeight is a parameter of the function)

Thanks

2
  • 1
    Not a javascript expert, but I believe the first will only return 24 if frameHeight is undefined where as the second will return 24 if frameHeight has any "falsey" value, like null, '', etc. Commented Aug 15, 2012 at 17:32
  • They are different, but if(frameHeight===undefined)frameHeight=24; IS equivalent but it is more readable and performs better since its value will only change when needed Commented Aug 15, 2012 at 17:48

4 Answers 4

9

Yes, they are different.

frameHeight = frameHeight || 24;

This will coerce frameHeight to a boolean value. If it is 0, '', false, null, undefined, or NaN it will be false and frameHeight will be defaulted to 24.

frameHeight = frameHeight !== undefined ? frameHeight : 24;

This will explicitly check if frameHeight is not undefined and ONLY for undefined will it default it to 24.

Sign up to request clarification or add additional context in comments.

Comments

2
frameHeight = frameHeight || 24;

^ Will do a null check as well. Will also do a check for 0, false, empty string ('') NaN and undefined

frameHeight = frameHeight !== undefined ? frameHeight : 24;

^ Will just check for undefined.

Comments

2

Yes, there is a difference and that difference can be significant depending upon the circumstances.

frameHeight = frameHeight || 24

will assign 24 to frame if frameHeight is initially ANY falsey value such as "", 0, null, undefined, NaN or false.

Whereas:

frameHeight = frameHeight !== undefined ? frameheight : 24

will only assign it 24 if the initial value is exactly undefined.

So, of possible significance in this particular function, the second method will allow you to pass 0 for the frameHeight to set a zero height, but the first method will not because it will override that 0 to 24.

Comments

0
frameHeight = frameHeight || 24;

fails for frameHeight = 0 works for frameHeight = null

frameHeight = frameHeight !== undefined ? frameHeight : 24;

fails for frameHeight = null works for frameHeight = 0

6 Comments

It will return 24 in both the cases of null and 0 for frameHeight = frameHeight || 24.
fails in the sense - In first case if value is 0 it will be reset to 24, but 0 is valid. If value is null it will be reset to 24, because null is invalid value. So fails for 0 works for null.
In second case, if value is 0 it won't be reset, because 0 is valid. If value is null it won't be reset, but null is invalid value. So works for 0, fails for null.
In my chrome dev tools when I do this: var a = 0; var c = a || 24. c equals 24.
Yes, but it is wrong logically because if frameHeight is 0, it should not change it to 24.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.