Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:38:20+00:00 2026-06-17T07:38:20+00:00

In the browsers I tested, var a = new Array(5); will return an Array

  • 0

In the browsers I tested, var a = new Array(5); will return an Array of 5 undefined elements. Since undefined equals to false I can consider my array to be initialized with false elements as long as I test them this way:

if (a[0]) console.log("the element is no longer falsy");
else a[0] = true;

Is it a safe way to initialize an Array with false values or is it just a hack that should be avoided?

EDIT: my goal is to have it initialized to falsy values as in my example. I want to make sure that new Array() is a trustworthy way to do so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:38:21+00:00Added an answer on June 17, 2026 at 7:38 am

    When you initialize an array in Javascript it has no elements, i.e. nothing is present in any position unless you put something there. The only thing you have is length set to the number you specify.

    When you access the element by reading it and nothing is there then you get undefined (as it happens when you acces a member of an object that doesn’t exist).

    Note however that an element that is not present at all is not exactly the same as an element that is set to undefined. For example:

    var x = new Array(5);
    console.log(x[2]);                 // ==> Output is "undefined"
    console.log(x.indexOf(undefined)); // ==> Output is "-1" (not present)
    x[2] = undefined;
    console.log(x.indexOf(undefined)); // ==> Output now is "2"
    

    If you only care about whether an element will be considered true when placed in an if then the array appears at a first sight to contain undefined that is false in that context.

    Note however that even what is false or true is quite a subtle concept in Javascript, especially because of automatic conversions… for example

    console.log([] ? 1 : 2);  // Output is "1", an empty array is "true"
    console.log([] == false); // output is "true" (so it's also equal to false!)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the sample code: var d = new Date(2010-06-09T19:20:30+01:00); document.write(d); On FF3.6 this will
On a lot of browsers I've tested, JavaScript blocks actually return a value. You
If I've tested my pages in most major browsers, why would I need to
This problem exists for webkit browsers only chrome and possibly safari (i've not tested
Possible Duplicate: Do most browsers automatically search for the favicon.ico? Will a favicon.ico that
<html><body><script> var matches = /(\w+)(\s*(\w+))?/.exec(aaa); alert(matches.length); alert(typeof(matches[3])); </script></body><html> I'm really new to regular expressions,
Consider the following example: <form action=process.php id=myForm> ..... ....... all my form elements </form>
In javascript, we can create a new DOM element in the following ways... By
<script language=JavaScript> <!-- Hide from older browser var x= new Date() var y= x.getYear()
When running the following script in Safari and Firefox 5 (other browsers not tested),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.