/    Sign up×
Community /Pin to ProfileBookmark

Explain var _t12 = _t12 || [];

I came across this question in a past exam paper and need someone to explain what does the following java script code do

var _t12 = _t12 || [];

var _t12 is a variable declaration. In my idea, this script creates an array with no array size (array size to be put inside the square brackets). But I’m not sure about the modulus operator (||);

In java (not javascript), someArray = new int [7]; declares and creates an array to hold 7 array elements.

Can someone tell me if i’m right or wrong and if wrong what does it do

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@mrhooFeb 04.2011 — var _t12 = _t12 || [];

set the variable _t12 to itself if it already exists and does not evaluate as false, or to an empty Array otherwise.

|| is not the modulus operator

a=[7] returns an array with a length of 1 that has the value a[0]=7.
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...