Variables Flashcards
What is a variable?
A variable is a “named storage” for data.
How do you create a Variable?
To create a variable, use the let keyword.
let message;
message = ‘Hello’;
how do you create an unchanging Variable?
To declare a constant (unchanging) variable, use const instead of let:
const myBirthday = ‘18.04.1982’;
There are two limitations on variable names in JavaScript:
The name must contain only letters, digits, or the symbols $ and _. The first character must not be a digit.
Case matters
Variables named apple and APPLE are two different variables.
Summary
let – is a modern variable declaration.
var – is an old-school variable declaration. Normally we don’t use it at all, but we’ll cover subtle differences from let in the chapter The old “var”, just in case you need them.
const – is like let, but the value of the variable can’t be changed.
Variables should be named in a way that allows us to easily understand what’s inside them.
What is Variable Scope?
The scope of a variable is the part of the program where the variable is visible. Variables declared with let or const are block-scoped. A code block is a portion of a program delimited by a pair of opening and closing curly braces { … }.
What are the main properties of a Variable?
A variable has three main properties:
1.) Its name, which identifies it. A variable name may contain upper and lower case letters, numbers (not in the first position) and characters like the dollar sign ($) or underscore (_). 2.) Its value, which is the data stored in the variable. 3.) Its type, which determines the role and actions available to the variable.
What are Type Conversions?
1.) An expression’s evaluation can result in type conversions. These are called implicit conversions, as they happen automatically without the programmer’s intervention.
(e.g)
const f = 100;
// Show “Variable f contains the value 100”
console.log(“Variable f contains the value “ + f);
2.) Sometimes you’ll wish to convert the value of another type. This is called explicit conversion. JavaScript has the Number() and String() commands that convert the value between the parenthesis to a number or a string.
(e.g)
const h = “5”;
console.log(h + 1); // Concatenation: show the string “51”
const i = Number(“5”);
console.log(i + 1); // Numerical addition: show the number 6