| Declaration |
Declaring a variable is the process of creating a variable. When a variable is declared
it doesn't have to be initialized immediately (default: undefined).
|
let name; // It's value is undefined
|
| Initialization |
Initializing a variable is the process of assigning an initial value to a variable. |
let name = 'John';
|
| Expression |
An expression is any valid unit of code that resolves to a value. This includes function
calls that return a value or expressions that consist
of multiple subexpressions.
|
const x = 5; // Resolves to 5
const y = getAnswer() // Resolves to a value
const z = (156 + 23) / 2 // Resolves to 89,5
|
| Statement |
A statement is (rouhgly) an instruction or an action. It performs actions or controls
actions, but doesn't resolve to a value. |
if (...) {
}
while (...) {
}
|
| Parameters |
Parameters are the variables in a function definition. They represent the values passed to
the function inside the function. |
function sum(a, b) {
return a + b;
}
|
| Arguments |
Arguments are the values we pass to a function when it is invoked. |
sum(4, 6);
|
| Instance |
An instance is in JavaScript an object that is created by using a constructor function. |
let john = new Employee();
|
| Properties |
Properties are the key: value pairs that are associated with a JavaScript object. |
const animal = {
legs: 4,
species: 'lizard',
tail: true
}; // Animal has 3 properties
|
| Method |
A method is a property which contains a function definition. |
const employee = {
name: 'John',
surname: 'Doe',
completeName: function() {
return name + ' ' + surname;
}
};
eployee.completeName();
|
| Null |
Null is a primitive data type. It represents the intentional absence of value. |
let x = null;
|
| Arithmetic Operators |
JavaScript supports the following arithmetic operators:
+ for addition
- for substraction
* for multiplication
/ for division
% for modulo (calculate rest)
|
// Addition
10 + 5
// Substraction
10 - 5
// Mulitplication
10 * 5
// Division
10 / 5
// Modulo
10 % 5
|
| Assignment Operators |
An assignment operator assigns a value to its left operand based on the value of its right
operand.
+= addition assignment
-= substraction assignment
*= multiplication assignment
/= division assignment
|
let number = 100;
// Both statements will add 10
number = number + 10;
number += 10;
console.log(number); // 120
|
| String Interpolation |
String interpolation is the process of evaluating string literals containing one or more
placeholders by using template literals.
|
let age = 20;
// String concatination
'Tommy is ' + age + ' years old.';
// String interpolation
`Tommy is ${age} years old.`;
|
| Variables |
Variables are used to store data. A variable contains data that can be used in the program
elsewhere. Using variables also ensures code re-usability.
|
const currency = '$';
const income = 90000;
console.log(currency + income ' is really a lot');
|
| Undefined |
undefined is a prmitive JavasScript value that represents the lack of a defined
value. Variables that are declared but not initialized will have the value
undefined.
|
let a;
console.log(a); // undefined
|
let Keyword |
let creates a local variable in JavaScript which can be re-assigned. It will
contain a value of undefined if nothing is assigned to it.
|
let count; // undefined
count = 10; // 10
|
const Keyword |
A constant variable can be declared using the const keyword. It as to be
initialized. Any attempt of re-assigning a const variable will result in a runtime error.
|
const numLegs = 2;
numLegs = 4; // ERROR
|
| Scope |
Scope is a concept that refers to where values and functions can be accessed. Scopes
include:
- Global scope - variables/functions declared here can be used anywhere in
the entire program.
- File/Module scope - value/funciton can only be accessed from within the
file.
- Function scope - only visible within the function.
- Code block scope - only visisble within a
{...} codeblock.
|
function sayName() {
const firstName = 'John';
// Here we can use firstName
}
// Here we can't use firstName
|
| Block Scoped Variables |
const and let are block scoped variables, which makes them
only accessible in their block or nested blocks. |
const isRegistered = true;
if (isRegistered) {
const statusMsg = 'This is a registered user.';
}
console.log(statusMsg); // ERROR
|
| Global Variables |
Variables that are declared outside of blocks or functions can exist in the global
scope which makes them accessible throughout a program.
Best practice is to reduce global variables to a minimum. |
const color = 'green'; // Global declaration
function printColor() {
console.log(color);
}
printColor();
|
| Ternary Operator |
The ternary operator allows for a compact syntax in case of binary decisions. It accepts a
condition followed by a ? operator, and
then two expressions seperated by a :. If the condition evalutes to truthy, the
first expression is executed, otherwise the second. |
let price = 20;
let day = 'Friday';
day === 'Friday' ? price -= 1.5 : price += 0.5;
|
| Arrow Functions |
The syntax for an arrow function doesn't require the function keyword and uses
a fat arrow => to separate the parameters from the body.
Keep in mind that arrow functions usually don't have their own this
scope.
There are several variations of arrow functions:
- Arrow functions with a single parameter do not require the
().
- Arrow functions with a single expression can use the concise function
body, which returns the result without using the
return keyword.
|
// Two arguments
const sum = (x, y) => {
return x + y;
}
// No arguments
const printName = () => {
console.log('John');
}
// Single argument
const checkTemperature = temp => {
return temp > 0 ? 'Above the freezing point' : ' Below the freezing point';
}
// Concise version
const multiply = (a,b) => a * b;
|
| Mutable |
JavaScript arrays are mutable, which lets us change the values they contain. Even if we
declare the array using the const
keyword, the contents can be manipulated by reassigning internal values or by using
.push() and .pop() methods. |
const names = ['John', 'Jane'];
names.push('Bob');
|
| Invoking, calling or applying |
When we are invoking, calling or applying a funciton we essential
execute it. A function is a piece of code or program that is wrapped in a value. A function
can be called by putting parentheses after the functions name. |
prompt('Enter your name please:'); // This will execute a built in function
|