Variables
In JavaScript, variables are used to store data values. They can be declared using the var
, let
, or const
keyword.
The var
keyword is used to declare a variable with function-level scope. This means that the variable is accessible within the function it was declared in, as well as any functions nested within it.
The let
keyword is used to declare a variable with block-level scope. This means that the variable is accessible within the block of code in which it was declared, as well as any blocks nested within it.
The const
keyword is used to declare a variable that cannot be reassigned. This means that once a value is assigned to a const
variable, it cannot be changed.
Here is an example of how to declare variables in JavaScript:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let x = 10; // Declares a variable x and assigns it a value of 10 const y = "hello"; // Declares a constant y and assigns it a value of "hello" var z = true; // Declares a variable z and assigns it a value of true
[/dm_code_snippet]
Operators
JavaScript has several types of operators that can be used to perform operations on variables and values. These include:
- Arithmetic operators:
+
,-
,*
,/
,%
(modulo),++
,--
- Comparison operators:
==
,!=
,===
,!==
,>
,<
,>=
,<=
- Logical operators:
&&
,||
,!
- Assignment operators:
=
,+=
,-=
,*=
,/=
,%=
Here are some examples of how these operators can be used:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let x = 10; let y = 20; console.log(x + y); // Outputs 30 (arithmetic operator) console.log(x == y); // Outputs false (comparison operator) console.log(x && y); // Outputs true (logical operator) x += 10; // x is now equal to 20 (assignment operator)
[/dm_code_snippet]
Control Structures
JavaScript has control structures that allow you to control the flow of your program. These include:
if
statements: These allow you to execute a block of code if a certain condition is true.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let x = 10; if (x > 5) { console.log("x is greater than 5"); } else { console.log("x is not greater than 5"); }
[/dm_code_snippet]
for
loops: These allow you to execute a block of code multiple times.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (let i = 0; i < 10; i++) { console.log(i); }
[/dm_code_snippet]
while
loops: These allow you to execute a block of code as long as a certain condition is true.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let y = 0; while (y < 5) { console.log(y); y++; }
[/dm_code_snippet]
do-while
loops: These are similar to while
loops, but the block of code is executed at least once before the condition is checked.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
do { console.log(y); y++; } while (y < 10);
[/dm_code_snippet]
switch
statements: These allow you to execute a block of code based on the value of a variable.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let z = "foo"; switch (z) { case "foo": console.log("z is foo"); break; case "bar": console.log("z is bar"); break; default: console.log("z is neither foo nor bar"); }
[/dm_code_snippet]
Tips and Best Practices
Here are a few tips and best practices to keep in mind when working with variables and operators in JavaScript:
- Variable names must start with a letter, dollar sign, or underscore, and can contain letters, digits, dollar signs, and underscores.
- JavaScript is case-sensitive, so
x
andX
are considered different variables. - JavaScript has dynamic typing, which means that the type of a variable can change at runtime. For example, you can assign a number to a variable, and then later assign a string to the same variable.
- You can use the
typeof
operator to determine the type of a variable. For example,console.log(typeof x)
will outputnumber
ifx
is a number, orstring
ifx
is a string. - The
+
operator can be used for both addition and concatenation. If both operands are numbers, it will perform addition. If at least one operand is a string, it will perform concatenation.