/    Sign up×
Community /Pin to ProfileBookmark

Javascript Array Basic Question

Hi, Im learning JavaScript but have a question about arrays, hope you can help:

I understand what an array/element etc is but can you explain to me the differences between declaring an array like this:

let logEntries = [ ]

or this:

let logEntries

logEntries = [ ]

One we are declaring logEntries as an array at the start in the variable area, and the other we are turning a standalone variable into an array, but what is the difference or best way? Are they any different programmically?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 07.2020 — From a functional view both versions behave identical. The first one is shorter and simpler and I would recommend to use ist. However there can be a reason to use the second one: If the variable needs to be global but it's value is not known at first and has to be assigned inside a function it can be appropriate to define it in the global first scope and later on assign the value in the limited scope of the function.
Copy linkTweet thisAlerts:
@VITSUSAMay 07.2020 — I agree with Sempervivum, both versions behave identical.
×

Success!

Help @WJC19 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.10,
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,
)...