What does this JavaScript code mean? What does this evaluate to and what do the parentheses do?
/**
* View Controller
* @type {Object}
*/
var controller = controller || {};
What does this JavaScript code mean? What does this evaluate to and what do the parentheses do?
/**
* View Controller
* @type {Object}
*/
var controller = controller || {};
var controller = controller || {};
So it simply means that if the controller is undefined
as default value {}
, will be initialised to that particular variable.
here ||
is simply an OR operator
which you might have used in conditional statements.
To avoid confusion, I will use different variable names:
var controller = cont || {};
This expression will check the value of cont
and if it is undefined
, it will assign {}
or an empty object to controller
. If cont
has a value, controller
will be assigned that value.