1

I have a code js;

<script type="text/javascript">
init_test(500,100);
document.write(init_test[0]);
</script>

but output is wrong, it is not result is 500. How to fix ?

1
  • It helps to keep the JavaScript error console open where error messages are displayed. Commented Jan 16, 2012 at 9:18

2 Answers 2

2

It looks like you intended init_test to be an array. Currently you are attempting to call a function named init_test with two arguments. What you wanted was this:

var init_test = [500, 100]; //init_test is an array with 2 elements
document.write(init_test[0]); //Write the element at index 0

If that's not what you intended, and init_test is a function that you haven't shown in your question, and that function returns an array or an object, you need to assign the return value to a variable and then access the index of that:

var returned = init_test(500, 100);
document.write(returned[0]);
Sign up to request clarification or add additional context in comments.

Comments

0

You tried to access on an variable which is maybe only declared within the function 'init_test()'. Define the variable outside your function and I'm sure you will get the correct value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.