Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does || {} means in JavaScript? [duplicate]

Tags:

javascript

I am confused with this code that I found in the Google Maps API:

window.google = window.google || {};
google.maps = google.maps || {};

And other code that I saw in a book:

var QQ = QQ || {};

What does it mean?

Why should we print that code at beginning of the JavaScript file?

like image 763
user3165083 Avatar asked Dec 20 '22 19:12

user3165083


1 Answers

This means that if window.google doesn't have value (undefined, null) then use {}.

It is a way of assigning a default value to a variable in JavaScript.

like image 61
brg Avatar answered Jan 07 '23 04:01

brg