Javascript cheat sheet

Javascript cheat sheet

Differences Between var, let, and const

Differencevarletconst
ScopeFunction scopeBlock scopeBlock scope
HoistingHoisted to the top of the scopeNot hoistedNot hoisted
ReassignmentCan be reassignedCan be reassignedCannot be reassigned
Value ImmutabilityValues can be changedValues can be changedValues cannot be changed
Global ObjectBecomes a property of the global object (e.g., window in a browser)Not a property of the global objectNot 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 TypeExample Values
number42, 3.14, -10
string"Hello, world!", 'JavaScript'
booleantrue, false
undefinedundefined
nullnull
symbolSymbol('example')
bigint12345678901234567890123456789n

Reference Data Types:

Data TypeExample Values
object{ name: 'John', age: 30 }
array[1, 2, 3, 4, 5]
functionfunction add(a, b) { return a + b; }
Datenew Date()
Custom Objectsconst person = { name: 'Alice', age: 25 }

Data types

ValueType
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 StatementPurposeExample
ifExecute code if a specified condition is true.javascript if (condition) { /* code */ }
elseExecute an alternative block of code if the if condition is false.javascript if (condition) { /* code */ } else { /* code */ }
else ifTest multiple conditions in a sequence.javascript if (condition1) { /* code */ } else if (condition2) { /* code */ }
switchCompare a value against multiple possible cases and execute code accordingly.javascript switch (value) { case case1: /* code */ break; case case2: /* code */ break; default: /* code */ }

Leave a Reply

Your email address will not be published. Required fields are marked *