0

I have this code -

var status = ["A", "B", "C", "D", "E", "F"];


$(function() {
  console.log(window.status);
  console.log(typeof window.status);
  var status = ["A", "B", "C", "D", "E", "F"];
  console.log(status);
  console.log(typeof status);

});
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Outside of $(function() {}); the var status is declared as an array. Yet when it comes to logging it to console, it shows up as a String.

I repeat with the same thing inside the ready function and it behaves as it should this time. What's wrong with this code?

4
  • When i run this code, it shows an array and an object. What is the problem ? Commented Jul 29, 2017 at 9:45
  • 1
    @DalvinderSingh Check the scrollbar ;) Commented Jul 29, 2017 at 9:46
  • for me it shows the log as - A, B, C, D, E, F and the typeof is showing string. Is this browser specific? The last two logs are showing it as an array, as they should. Commented Jul 29, 2017 at 9:46
  • 1
    I edited the OP so you don't need to scroll in the snippet ;-) Commented Jul 29, 2017 at 9:47

2 Answers 2

3

window.status was the property which defined the browser's status bar text (a string). Therefore your array got automatically converted to a string.

You could prevent that from happening if you move status into its own closure, for example by wrapping it within a (function(){...})(); and accessing it by status, not window.status.

Sign up to request clarification or add additional context in comments.

Comments

2

Check with other name its using default by javascript

var my_status = ["A", "B", "C", "D", "E", "F"];


$(function() {
  console.log(window.my_status);
  console.log(typeof window.my_status);
  var status = ["A", "B", "C", "D", "E", "F"];
  console.log(status);
  console.log(typeof status);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

Thanks, I did not realize that status was taken by JS.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.