Variables are used to store values.
Variables can be thought of like a box in the ‘real world’.
A box can hold items and can be labelled so we know what is in it.
If we create a variable and label it “firstName”
let firstName = "Jonas"
Now if we “call” or publish “firstName” we will get a value of “Jonas”
This can be extremely useful. For example, if you were to use “firstName” in hundreds of places on a website, you can just change the variable value, and in theory, all the ‘firstNames’ will update with one change in the code.
Naming Conventions for Variables
Camel case involves having no spaces between words and leaving the first letter lowercase.
for example – firstNamePerson is written in camel case.
I guess the capital letter looks a bit like a camel’s hump.
This isn’t a hard rule, but general practice.
Hard rules for Variable Names
Variable names can’t start with a number – e.g. 3years = 3; – would result in an error because of the number “3” in the name
Symbols are generally a bad idea in variable names. You can only use letters, numbers, underscores or dollar sign.
You can’t use reserved JS keywords. e.g. “new” is a reserved keyword, as is “function” and “name”.
Don’t use all uppercase letters for variable name either. Unless it is a “constant” that never changes, such as the value of PI
You declare variables with “let”, you can update the variable later, without using “let”.
Constant – used to declare variables that don’t change e.g. date of birth
Strings and Template Literals
Good tutorial here.
Template literals make it easier to build strings.
Template Literals allow you to create:
- Multiline strings – strings (text) that spans several lines
- String Formatting – you can change part of the string for values of variables – This is called “String Interpolation”
- HTML Escaping – making it okay to include in HTML of a webpage
let str = `Template literal here`;
The template literals allow you to define multiline strings more easily because you need to add a new line in the string wherever you want:
let p = `This text can span multiple lines`;
Type Conversion and Coercion
Type coercion, type conversion, typecasting, and type juggling: all different names that refer to the process of converting one data type into another. This process is present in almost every programming language and is an important concept in computer science.
What is Type Conversion?
Type conversion can be :
implicit – done automatically by the code already in place
explicit – done more manually by the developer
What is Type Coercion?
When coercion is done automatically, it can cause some weird outcomes and issues
Values are considered either truthy (evaluate to
true) or falsy (evaluate to
false) depending on how they are evaluated in a Boolean context.
In JS there are 6 incidences that result in, or are considered “Falsyies”
- The primitive value
- The primitive value
- The empty string (
- The global property
- A number or BigInt representing
- The keyword
All other values are considered “truthys”
truthyOrFalsy(undefined); // Falsy Value truthyOrFalsy(NaN); // Falsy Value truthyOrFalsy(null) // Falsy Value truthyOrFalsy(""); // Falsy Value truthyOrFalsy(false) // Falsy Value truthyOrFalsy(0); // Falsy Value truthyOrFalsy(-0); // Falsy Value truthyOrFalsy(0n); // Falsy Value
Equality Operators == and ===
The operator using “two equals signs”, “==” is a “loose equality operator”. It will try and convert (using “coercion”) the values and then compare them.
Genearlly, or loosely speaking, “==” just checks the values.
The ‘===’ operator, is the “strict operator” and checks the value and the data-type are the same.
Boolean Logic is a form of algebra that is centered around three simple words known as Boolean Operators: “Or,” “And,” and “Not.” These Boolean operators are the logical conjunctions between your keywords in a search to help broaden or narrow its scope.https://www.lotame.com/what-is-boolean-logic/
Boolean logic dictates that all values are either true or false.
Boolean uses operators such as “AND” and “OR”.
For the result to be true in the example shown above, both A and B in the example, need to be true.
With the “OR” operator, we just need A or B to be true, for the result to be “TRUE”.
NOT operators invert the logical operators.
falseif its single operand can be converted to
true; otherwise, returns
The Switch Statement
A quick, or easier way of doing a complicated “if else” statement.
The switch statement can be used so that you can perform different actions, when one of a range of conditions is met.
If the condition – e.g. “Monday” in the screenshot above, is met, then the code associated with Monday, will be executed.
If there is no match at all, then the default code, shown at the bottom left of the screenshot above, will be executed.
You can also use the if/else statement as an alternative to the switch statement
If Else alternative to Switch Statement
Statements & Expressions
An expression is a piece of code that produces a value. e.g. 3 + 4 is an expression, because it creates a value.
Numbers themselves are expressions.
Booleans, which produce “true or false” is an expression
A statement, does not produce a value.
The Conditional (Ternary) Operator
The conditional operator, allows us to write something similar to an “If Else Statement”, but all in one line.
Use a question mark ? after the condition is declared, and that write the code we want executed if condition is true.
The “else” block, or the equivalent of an else block, goes after a colon :
To activate “strict mode”, right at the top of your script, you have to type:
As you would expect,
Strict mode, helps developers identify mistakes and bugs in the code.
Strict mode doesn’t allow you to do certain (incorrect) things and in other instances, it will visibly show you that you’ve created an error.
For example, if you spell a variable incorrectly,