Variables In JavaScript
Tue Jun 06 2023 16:11:37 GMT+0000 (Coordinated Universal Time)
Saved by
@Vkmartinez95
#javascript
#innerhtml
#button
#onclick
#html
//Example Variables
var x = 5;
var y = 6;
var z = x + y; // or 11
// Same result, but "let"
let x = 5;
let y = 6;
let z = x + y;
// Again, but "const"
const x = 5;
const y = 6;
const z = x + y;
//Mixed Concept
const price1 = 5;
const price2 = 6;
let total = price1 + price2;
// You can declare many variables in one statement.
// Start the statement with let and separate the variables by comma:
let person = "John Doe", carName = "Volvo", price = 200;
// A declaration can span multiple lines:
let person = "John Doe",
carName = "Volvo",
price = 200;
// JavaScript Dollar Sign $
// Since JavaScript treats a dollar sign as a letter, identifiers containing
// $ are valid variable names:
let $ = "Hello World";
let $$$ = 2;
let $myMoney = 5;
//In JavaScript, the equal sign (=) is an "assignment" operator, not an "equal to" operator.
//This is different from algebra. The following does not make sense in algebra:
x = x +
// JavaScript variables can hold numbers like 100 and text values like "John Doe".
// In programming, text values are called text strings.
// JavaScript can handle many types of data, but for now, just think of numbers and strings.
// Strings are written inside double or single quotes. Numbers are written without quotes.
// If you put a number in quotes, it will be treated as a text string. Example :
const pi = 3.14;
let person = "John Doe";
let answer = 'Yes I am!';
// As with algebra, you can do arithmetic with JavaScript variables, using operators like = and + :
let x = 5 + 2 + 3;
// You can also add strings, but strings will be concatenated:
let x = "Venus" + " " + "Martinez";
// Also try this :
let x = "5" + 2 + 3; // Hint #523
content_copyCOPY
## *All* JavaScript variables must be identified with unique names.
### These unique names are called *identifiers*.
Identifiers can be short names (*like x and y*) or more descriptive names (*age, sum, totalVolume*).
#### The general rules for constructing names for variables (*unique identifiers*) are:
• Names can contain letters, digits, underscores, and dollar signs.
• Names must begin with a letter.
• Names can also begin with $ and _ (but we will not use it in this tutorial).
• Names are case sensitive (y and Y are different variables).
• Reserved words (like JavaScript keywords) cannot be used as names.
https://www.w3schools.com/Js/js_variables.asp
Comments