0

javaScript(variable)

var s = s || {};
s.c = {};

what purposes it will be use?

ML680
  • 99
  • 1
  • 1
  • 10

1 Answers1

4

var s = s || {};

This means that if s is null, undefined or false (it computes to false), then an empty object {} will be assigned to s, so that the second line would not cause an error.

But this notation is inacurate. It should be something like:

var s = (typeof s == 'object') ? s : {};

because in the first example if s is a number the second line will still cause an error.

In the second example notation A ? B : C; is equal to:

if(A){
    B;
}else{
    C;
}
jdabrowski
  • 1,805
  • 14
  • 21