I have a piece of code below that says "Unexpected assignment expression." where (foo = bar).
function bar() {}
var foo;
foo || (foo = bar);
I have tried to create a variable before it and assign it to the variable. But it gives another error where foo is saying "Expected an assignment or function call and instead saw an expression." where foo || foobar (foobar is the variable i had to create when i tried var foobar = bar) (basically foo || foobar). I keep running into a wall trying to figure this one out. How would you recode this lint free?
Thanks.
javascript linting error
Re: javascript linting error
Code: Select all
foo = foo === undefined ? bar : foo;
Fighting for peace is declaring war on war. If you want peace be peaceful.
Re: javascript linting error
Would this be the same?
if (foo === undefined) {
bar;
} else {
foo;
}
if (foo === undefined) {
bar;
} else {
foo;
}
- KyleMassacre
- Posts: 573
- Joined: Wed Nov 27, 2013 12:42 pm
Re: javascript linting error
I don't believe that would work either.Mardonis wrote:Would this be the same?
if (foo === undefined) {
bar;
} else {
foo;
}
I think you are looking for something like this if you are not a fan of ternaries:
Code: Select all
if(foo === undefined)
foo = bar;
Re: javascript linting error
ok cool, thank you. I was basically trying to NOT use ternaries like you said. wanted to get it back to a normal if statement for readability. Then I was going to optimize later.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: javascript linting error
I have always loved ternaries and find them very quickly readable compared to an IF statement, but to each their own
The indelible lord of tl;dr