Chapter 2 - Variables for strings
Chapter 2 - Variables for strings
Example:
var name = "Mark";
So, now name means "Mark" in JavaScript. Whenever you use name,
JavaScript understands that it refers to "Mark".
Instead of writing "Mark" every time, you can just write name. This
makes coding easier and more flexible.
Example:
alert(name); // This will show a pop-up with "Mark"
A variable does not have to keep the same value forever. You
can change it later.
Example:
var name = "Mark";
name = "Ace";
alert(name); // Now it shows "Ace" instead of "Mark"
Example:
var nationality; // The variable is created but empty
nationality = "U.S."; // Now it has a value
This is useful if you don’t know the value at first but will add it later.
You can name a variable anything (as long as it follows the rules).
var floogle = "Mark";
var x = "Mark";
var lessonAuthor = "Mark";
var guyWhoKeepsSayingHisOwnName = "Mark";
👉 JavaScript does not care what you name your variables, but
you should choose meaningful names so your code is easy to
understand.
A text string (words inside " " or ' ') always needs quotation marks.
A variable never uses quotation marks.
Example:
var nickname = "Bub"; // Correct (nickname is a variable, "Bub" is a string)
alert(nickname); // Shows Bub
Final Summary