0

In Ruby you have something called a conditional assignment (||=). You can use it to assign values to variables if they have not already been defined or set. It is similar to saying this in Javascript:

if (typeof x == undefined) { x = 'value' }

In Ruby, to assign 'value to x if it has not already been assigned, you would use x ||= 'value

Example of how it works:

x = 1
x ||= 2
return x    // Will return 1

Or:

x ||= 2
return x    // Will return 2

Is there any way to do this in Javascript without the significantly longer if statement?


Solved: @tede24 suggested the following

var x = x || newValue

The only way it is different to Ruby is that if x has been defined to 0, i.e. var x = 0, the will set x to newValue


Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
  • 1
    You should not use variables named `var`. –  Feb 01 '16 at 13:45
  • Sorry, I don't, just when I was typing the example quickly I accidentally used `var` instead of `x` – Kaspar Lee Feb 01 '16 at 14:08
  • Please do not add solutions to questions. If an existing answer does not satisfy you, then write your own answer. If you have comments on someone else's answer, then add it as a comment on that answer. –  Feb 01 '16 at 15:52
  • Here's a really old thread on this topic: https://esdiscuss.org/topic/logical-assignment-operators. Slightly newer: https://esdiscuss.org/topic/is-much-needed. –  Feb 01 '16 at 16:01
  • By the way, `typeof x == undefined` is incorrect. It should be `typeof x == "undefined"`, with `undefined` in quotes. –  Feb 01 '16 at 17:47

2 Answers2

3
x = typeof x !== 'undefined' ? x : newValue;

UPDATE : tede24 has better answer, i was just trying to learn - How to get rid of reference error, if used without var or already not declared

Oxi
  • 2,918
  • 17
  • 28
  • Yes, this does work, though @tede24 answer was more efficient. However, in @tede24's answer, if `x` has been assigned before it is reassigned using `var x = newValue`, whereas yours will just do `x = newValue`. So if using `var` to re-assign a variable is incorrect, then your answer it the best one here. – Kaspar Lee Feb 01 '16 at 14:12
  • Well, what can I say :) – Oxi Feb 01 '16 at 14:14
  • No, this answer is better. This is actually the code that Babel would generate for a default parameter. –  Feb 01 '16 at 16:02
  • @torazaburo I did specifically ask for an answer **without** the `if` statement though (read my question carefully), so this is **not** the best answer – Kaspar Lee Feb 01 '16 at 17:16
  • The accepted answer is not equivalent to the Ruby you mention, because it re-assigns `x` if it has any falsy value, including 0, empty string, etc. –  Feb 01 '16 at 17:28
2
var x = x || newValue;

This mean whenever x has something being evaluated as false (undefined, null, 0), it will assign the second value.

tede24
  • 2,304
  • 11
  • 14
  • x == x || newValue; // Uncaught ReferenceError: x is not defined – Oxi Feb 01 '16 at 10:59
  • No, `x` has not always been declared before (I edit my question, I realize I said "set" rather than "declared") – Kaspar Lee Feb 01 '16 at 11:03
  • Sorry corrected a typo = instead of == – tede24 Feb 01 '16 at 11:07
  • It does, unless x is assigned `false` or `0`. In Ruby, it will not set `newValue` even if `x == false` or `x == 0`. Check this [JSFiddle](https://jsfiddle.net/rjtvadym/) – Kaspar Lee Feb 01 '16 at 11:16
  • Actually, Ruby will return `newValue` is `x == false`, however if it is `0` it will return `0`. [Check this](http://labs.codecademy.com/DLrb/3#:workspace) – Kaspar Lee Feb 01 '16 at 11:18
  • Yes I stated 0 behavior in the answer. This is what js does. If you need to replicate ruby behavior you will need to use a function to make comparisons and return what you need – tede24 Feb 01 '16 at 12:06
  • @tede24, Yes, it is stated in your answer, thanks! – Kaspar Lee Feb 01 '16 at 14:10
  • 1
    @torazaburo the question states the variable might be not defined or not set. I agree if you know x is defined you shouldn't re define it, but if you don't know (this case) this is the way to go. – tede24 Feb 01 '16 at 15:46
  • *If you need to replicate ruby behavior you will need to use a function to make comparisons and return what you need.* But that is what the question was asking you to do! I am confused. @Druzion appears to have accepted an answer because at least it explicitly states that it does not actually do what he asked for in his question? –  Feb 01 '16 at 17:41
  • @torazaburo It does exactly want I want it to do. – Kaspar Lee Feb 02 '16 at 10:38
  • Then your question is framed incorrectly. You said "Is there any way to do **this**", where **this** is presumably the Ruby conditional assignment. –  Feb 02 '16 at 12:23