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

  • SEARCH
  • Home
  • 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 9156913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:53:05+00:00 2026-06-17T12:53:05+00:00

Here is code that I have copied from w3schools, I have different code, but

  • 0

Here is code that I have copied from w3schools, I have different code, but the problem that I am having still happens in this simplified coding.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to create an array, then display it's length</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var foods = ["steak","pizza","bread","fruits"];
var x=document.getElementById("demo");
var y=foods[3];
x.innerHTML= y.length;
}
</script>

</body>
</html>

When I do x.innerHTML= fruits.length;, I get 4 back which is what I want to get.

But when I call

var y=foods[3];
x.innerHTML= y.length;

I get 6 which is the length of the word "fruits" but I want the length of the array fruits.

How do I do this?

I’m using jQuery, don’t know if that affects anything. Do I have to add parenthesis or brackets somewhere?

  • 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-17T12:53:06+00:00Added an answer on June 17, 2026 at 12:53 pm

    If (and only if) a variable (myVar) contains the name of a property of another object (myObj) you can use:

    myObj[myVar]
    

    to access that property.

    In this case, your fruits array is just a normal local variable, so there’s no way (short of the frowned-upon eval function) to access it indirectly.

    You can of course use fruits.length to directly find its length.

    A better solution would be a nested object of foods, and not an array:

    var foods = {
        fruits: [ "Banana", "Orange", "Apple", "Mango" ],
        steak: [ ... ],
        pizza: [ ... ],
        bread: [ ... ]
    };
    

    at which point you can use the syntax above and write:

    var myType = 'fruits';
    var count = foods[myType].length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea what this means. But here is the code that it
enter code here I have a ListView that have a different Layout XML for
This is my code i have copied this code from somewhere to learn about
Here is a code that I copied from the web /** * A simple
I have this html page I copied from here . There is a javascript
I have copied this question here from spring forum . I have a parent
Here is the code that I have for my cell of my UITableView: forKeys:[NSArray
Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
I have some code here that uses bitsets to store many 1 bit values

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.