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

Popular posts from this blog

gcc - MinGW's ld cannot perform PE operations on non PE output file -

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

c# - Search and Add Comment with OpenXML for Word -