-1

Been trying for about an hour now, can't get my array to show its length upon a button press. It did show its length at one attempt, but it was a totally different amount to the amount of elements in the array. I read somewhere that it happens that way sometimes, and has something to do with undefined elements. But now it isn't showing any alerts at all.

var fruits("Banana", "Apple", "Orange")

function myFunction() {
  alert(fruits.length)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>

  <body>
    <script>
      src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" >
    </script>
    <script src="Arrays.js">
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
</head>
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
</body>

</html>
garfbradaz
  • 3,424
  • 7
  • 43
  • 70
BrownEyedNinja
  • 107
  • 1
  • 1
  • 9

6 Answers6

4

If you want to declare an array using () you need to use new Array():

var fruits = new Array("Banana", "Apple", "Orange")

function myFunction() {
  alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
2

var fruits = new Array("Banana","Apple", "Orange" );
var fruitsArr = ["Banana","Apple", "Orange" ];

function myFunction(arr) {
console.log(arr.length)
}

myFunction(fruits)
myFunction(fruitsArr)

You can create an array using array literal i.e [] or you can use new Array() to create an array.

Durga
  • 15,263
  • 2
  • 28
  • 52
1

Your array declaration is JavaScript code is incorrect. It should be like.

var fruits = ["Banana","Apple","Orange"]

var fruits = ["Banana", "Apple", "Orange"];

function myFunction() {
  alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()">Find Length of Array</button>
Geeky Ninja
  • 6,002
  • 8
  • 41
  • 54
  • 1
    @draneol Its the way you are declaring your array, var fruits ("Banana","Apple", "Orange") is not the correct way of declaring array. Instead, you can declare array by two ways 1) using [ ] brackets (as I have used in example) 2) or using new keyword e.g. var fruits = new Array('a','b'). – Geeky Ninja Jul 20 '17 at 09:02
0

The array literal syntax in JavaScript uses square brackets:

var fruits = [ "Banana","Apple", "Orange" ]
hellojeffhall
  • 291
  • 3
  • 8
0
var fruits = ["Banana","Apple", "Orange"]

Your code has something wrong.

chen lin
  • 21
  • 4
0

You need to use () with new Array() or you can use [] if you do not want to declare it explicitly. e.g.

var fruits = new Array("Banana", "Apple", "Orange")

OR

var fruits = ["Banana", "Apple", "Orange"]

var fruits = ["Banana", "Apple", "Orange"]

function myFunction() {
  alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>

OR

var fruits = new Array("Banana", "Apple", "Orange")

function myFunction() {
  alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
kk.
  • 3,747
  • 12
  • 36
  • 67