- Local variables: Are variables that are declared within a block or function.
- Global variables: Global variables are declared outside of a function.
// How to create variables: var x; let y; const z; // How to use variables: x = 5; y = 6; let z = x + y;
Fixed values are called literals. Variable values are called variables.
Number literals can be written with or without decimal places. Number literals can be either positive numbers or negative numbers. If you do not specify a sign, then a positive number is assumed.
8.50 2001 -24
String literals are text and are always surrounded by single quotes (
') or double quotes (
'Joe Bloggs' "Joe Bloggs"
Variables are used in programming languages to “store” data values.
To “assign values” to variables, use the equal sign (
// Declaring a variable let x; // Assigning a variable x = 8;
Here is a list of the different types of operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Conditional Operators
- Type Operators
Example of an arithmetic operator (
2 + 3; // 5
=) to assign values to variables:
let x, y; x = 5; y = 6;
A set of values, variables, and operators combined to produce a value is called an expression.
An evaluation is the name of the calculation.
// For example, 2 * 10 evaluates to 20: 2 * 10
Expressions can also contain variable values:
x * 10
let keyword tells the browser to create variables:
let x, y; x = 6 + 6; y = x * 10; //Evaluates to 120
Code that follows a double slash
// or that is between a
/* is regarded as a comment.
Comments are disregarded and won’t be executed:
let n = 5; // I will be executed // n = 10; I will NOT be executed
- A letter (
- An underscore (
- or the dollar sign (
Characters beyond that could be letters, numbers, underscores, or dollar signs.
For example, myVariable is not the same as myvariable. If you have problems in your code, check the case!
Example, the variables firstName and firstname, are two different variables:
let firstName, firstname; firstName = "Joe"; firstname = "Andy";
- A common naming convention that employs the practice of writing phrases with each word or abbreviation in the centre beginning with a capital letter e.g. myVariable
- Helps to join multiple words together without using underscores
- Avoids the use of the hyphen (-) character which isn’t allowed in many languages
- Provides the ability to create very descriptive variable names:
If you liked this article, then please share. You can also find me on Twitter for more updates.