Javascript cheat sheet
Differences Between var, let, and const
Difference | var | let | const |
---|---|---|---|
Scope | Function scope | Block scope | Block scope |
Hoisting | Hoisted to the top of the scope | Not hoisted | Not hoisted |
Reassignment | Can be reassigned | Can be reassigned | Cannot be reassigned |
Value Immutability | Values can be changed | Values can be changed | Values cannot be changed |
Global Object | Becomes a property of the global object (e.g., window in a browser) | Not a property of the global object | Not a property of the global object |
Hoisting
Hoisting in JavaScript is a mechanism by which variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is actually executed. This means that you can use variables and functions in your code before they are declared in the source code.
Here’s an example to illustrate hoisting with variable declarations:
console.log(x); // undefined
var x = 5;
console.log(x); // 5
In this example, even though we try to log the value of x
before it’s declared, it doesn’t result in an error. The variable declaration is hoisted to the top of its containing scope, so the first console.log
statement is essentially interpreted like this:
var x;
console.log(x); // undefined
x = 5;
console.log(x); // 5
Primitive and reference data types
- Primitive data types are like simple building blocks that hold basic values like numbers or text, and they can’t change once created.
- Reference data types are more like containers that can hold complex data, and they can change or point to different data over time.
Primitive Data Types:
Data Type | Example Values |
---|---|
number | 42 , 3.14 , -10 |
string | "Hello, world!" , 'JavaScript' |
boolean | true , false |
undefined | undefined |
null | null |
symbol | Symbol('example') |
bigint | 12345678901234567890123456789n |
Reference Data Types:
Data Type | Example Values |
---|---|
object | { name: 'John', age: 30 } |
array | [1, 2, 3, 4, 5] |
function | function add(a, b) { return a + b; } |
Date | new Date() |
Custom Objects | const person = { name: 'Alice', age: 25 } |
Data types
Value | Type |
---|---|
42 | “number” |
“Hello” | “string” |
true | “boolean” |
{} | “object” |
[] | “array” |
function(){} | “function” |
undefined | “undefined” |
null | “object” |
Symbol() | “symbol” |
123n | “bigint” |
Conditionals
In JavaScript, conditionals are used to make decisions in your code based on whether certain conditions are true or false. The primary conditional statements are:
Conditional Statement | Purpose | Example |
---|---|---|
if | Execute code if a specified condition is true. | javascript if (condition) { /* code */ } |
else | Execute an alternative block of code if the if condition is false. | javascript if (condition) { /* code */ } else { /* code */ } |
else if | Test multiple conditions in a sequence. | javascript if (condition1) { /* code */ } else if (condition2) { /* code */ } |
switch | Compare a value against multiple possible cases and execute code accordingly. | javascript switch (value) { case case1: /* code */ break; case case2: /* code */ break; default: /* code */ } |