Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active March 14, 2023 22:26
Show Gist options
  • Save sandrabosk/0fe29151dc48d0068ffdae1b7068ae21 to your computer and use it in GitHub Desktop.
Save sandrabosk/0fe29151dc48d0068ffdae1b7068ae21 to your computer and use it in GitHub Desktop.
// ************************************
// ************ BOOLEAN ***************
// ************************************
Can have 2 values:
- true
- false
We will heavily use booleans within conditional statements and for that purpose we need to learn
about logical operators: OR (||), AND (&&), NOT (!).
When using OR, the expression will always be TRUE unless both (left and right hand side we compare) are false:
true || true; // => true
true || false; // => true
false || true; // => true
false || false; // => false
When using AND, the expression will only be TRUE if both (left and right hand side we compare) are true:
true && true; // => true
true && false; // => false
false && true; // => false
false && false; // => false
NOT operator is used to negate the value of an expression.
!true; // => false
!false; // => true
----------
UNDEFINED
primitive value automatically assigned to variables when they are declared.
let name;
console.log(name); // <== undefined
----------
NULL
in computer science a value used to indicate that something is not even created (declared) yet.
in JavaScript, null is often used to represent value unknown variables (when we are unsure of the variable value):
let name = null;
console.log(name); // <== null
-----------
Assignment vs. comparison equal sign
// == compares just using value (loose or abstract equality)
let x = 5;
let y = "5";
console.log(`What is this: ${x == y}`); // What is this: true
// === compares using value and type (strict equality)
console.log(`What is this: ${x === y}`); // What is this: false
----------
FALSY and TRUTHY values
// falsy values - will evaluate as FALSE
// - false
// - 0
// - "" empty string
// - null
// - undefined
// NaN
// truthy values - will evaluate as TRUE
// true
// "0"
// "false"
// [] or {}
// 35 (any number)
// new Date()
if('false') `Passed thingy is truthy`
else `Passed thingy is falsy` // => 'Passed thingy is truthy'
// true && true
// true && false
// !true
if(true || false){
console.log("sooo true");
} else {
console.log("maybe not so true")
}
// ************************************
// ********* IMMUTABILITY *************
// ************************************
// All primitive data types are immutable - meaning, once a primitives are created, they can't be modified.
// Immutability means that once one of the primitive values is created, it can’t be modified.
// Example from lesson:
let city = "miami";
console.log(city[0]); // <== m
city[0] = "M"; // let's capitalize the first letter
console.log(city); // <== miami (it seems that the first letter is not capitalized after all since the primitive (string) can't be mutated
// Values are immutable but variables are mutable which means you can reassign them:
let city = "miami";
console.log(city); // <== miami
// we CAN re-assign our variable to another value
city = "berlin";
console.log(city); // <== berlin
// but still CAN NOT change the value "berlin"
city[0] = "B";
console.log(city); // <== berlin
// You can reassign the variable with a new value but you can't alter the existing value.
// Example with the string methods - each of them returns a new string and the original string stays untouched.
const message = "Don't be sad, be happy!";
console.log(message.slice(0,3)); // <== Don
console.log(message); // <== Don't be sad, be happy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment