Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="javascript variables">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>Variables</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*
- * Variables:
- *
- * Variables are used to hold things in memory.
- * They are named at declaration and assigned a value at initialization/assignment
- */
- // To declare a variable:
- var myVar;
- // To assign a variable:
- myVar = 'some value';
- // note: can be combined where on the left side of the equal sign the variable is declared and on the right side the variable is assigned:
- var myOtherVar = 'some other value';
- // Trying to access a variable that has been declared, but not assigned will reture undefined:
- var theVar;
- console.log(theVar); // > undefined
- theVar = 2;
- console.log(theVar); // > 2
- // Variables can be re-assigned to another value:
- console.log(theVar);
- theVar = 5.5; // theVar is now set to 5.5 not 2
- console.log(theVar);
- // When variables are assigned, the are given a type, and at re-assignment the variable can be re-assigned to any type
- theVar = "a string";
- theVar = [];
- theVar = {};
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*
- * Variables:
- *
- * Variables are used to hold things in memory.
- * They are named at declaration and assigned a value at initialization/assignment
- */
- // To declare a variable:
- var myVar;
- // To assign a variable:
- myVar = 'some value';
- // note: can be combined where on the left side of the equal sign the variable is declared and on the right side the variable is assigned:
- var myOtherVar = 'some other value';
- // Trying to access a variable that has been declared, but not assigned will reture undefined:
- var theVar;
- console.log(theVar); // > undefined
- theVar = 2;
- console.log(theVar); // > 2
- // Variables can be re-assigned to another value:
- console.log(theVar);
- theVar = 5.5; // theVar is now set to 5.5 not 2
- console.log(theVar);
- // When variables are assigned, the are given a type, and at re-assignment the variable can be re-assigned to any type
- theVar = "a string";
- theVar = [];
- theVar = {};
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement