javascript - What happens if you set a declared variable to a different type? -
var foo = 4; foo = "string"
what happen here?
foo
gets new value, "string"
. javascript loosely-typed language, variables (and object properties) not restricted holding single type of value during lifecycle.
gratuitous example:
var foo = 42; snippet.log(typeof foo); // "number" foo = "the question of life, universe, , everything"; snippet.log(typeof foo); // "string"
<!-- script provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Comments
Post a Comment