The + has been overloaded to do different things
let subtractionTest = "5" - 3;
console.log("subtractionTest is ");
console.log(subtractionTest);
let additionTest = "5" + 3;
console.log("additionTest is ");
console.log(additionTest);
/*
subtractionTest is
2
additionTest is
53
*/
In javascript, a true hash is an object with function
someVar = new Map();
someVar.name = "Bob";
someVar.age = 24;
console.log(JSON.stringify(someVar));
//OUTPUT IS...
/*
{"name":"Bob","age":24}
*/
console.log(someVar.get("name"));
//OUTPUT IS...
/*
undefined
*/
someVar.set("name", "Maxwell");
someVar.set("age", 64);
console.log(JSON.stringify(someVar));
//OUTPUT IS...
/*
{"name":"Bob","age":24}
*/
for (const [key, value] of Object.entries(someVar)) {
console.log(key, value);
}
//OUTPUT IS...
/*
name Bob
age 24
*/
for (const [key, value] of someVar) {
console.log(key, value);
}
//OUTPUT IS..
/*
name Maxwell
age 64
*/
const nextVar = new Map();
nextVar.set("name", "Jane");
nextVar.set("age", 21);
console.log(JSON.stringify(nextVar))
//OUTPUT IS...
/*
{}
*/
console.log(JSON.stringify(Object.fromEntries(nextVar)));
//OUTPUT IS
/*
{"name":"Jane","age":21}
*/
pass-by-value for primitives, pass-by-value of the reference for objects
let a = 10;
function change(x) {
x = 20;
}
change(a);
console.log(a); // 10 is unchanged, because x is a copy of the value
let obj = { name: "Alice" };
function modify(o) {
o.name = "Bob"; // modifies the original object
}
modify(obj);
console.log(obj.name); // "Bob" did we pass a reference?
function reassign(o) {
o = { name: "Charlie" }; // creates a new object locally
}
reassign(obj);
console.log(obj.name); // still "Bob" o was a copy of the reference!
This or this or?
const person = {
name: "Alice",
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
person.greet(); // Hello, my name is Alice" (this = person)
const greetFunction = person.greet;
greetFunction(); // Hello, my name is undefined" (this = global or undefined in strict mode)