Javascript Flashcards
Implementing a function in JavaScript and call the function
function greet(name) {
console.log(Hello, ${name}!
);
}
// Call the function
greet(“Camila”); // Output: Hello, Camila!
how to console.log a value
console.log(Hello, ${name}!
);
how to put default parameters
function greet(name = “Guest”) {
console.log(Hello, ${name}!
);
}
how to call it with desired parameters?
greet(“Camila”); // Output: Hello, Camila!
mention 7 array methods ( do not need to explain )
map()
filter()
reduce()
forEach()
sort()
slice()
splice()
mention 4 strings methods ( do not need to explain )
split()
join()
substring()
slice()
mention 3 Object Methods
Object.keys()
Object.values()
Object.entries()
mention 5 loops
for Loop
while Loop
do…while Loop
for…of Loop
for…in Loop
steps to approach a new problem (4)
- declare a function, name it, call it
- send input as default params
- write in comment which is datatype of input/output
- console.log the desired return
- detect with data type is the input, think of avaiable methods/loops
diference de ! y !!
! (Logical NOT):
The ! operator negates a value. It first coerces the value into a boolean, and then it inverts
console.log(!0); // true (0 is falsy, so !0 is true)
console.log(!’hello’); // false (non-empty string is truthy, so !’hello’ is false)
!! (Double Logical NOT):
console.log(!!0); // false (0 is falsy, so !!0 is false)
console.log(!!’hello’); // true (non-empty string is truthy, so !!’hello’ is true)
diference de == y ===
==: Realiza comparación flexible con conversión de tipos implícita.
===: Realiza comparación estricta sin conversión de tipos, verificando tanto el valor como el tipo.
console.log(5 == ‘5’); // true (porque ‘5’ se convierte a número)
console.log(null == undefined); // true (considerados iguales en comparación débil)
console.log(0 == false); // true (0 y false son considerados iguales)
check if its an array
Array.isArray(cleanWord)
how to remove a space of letter with js?
let result = str.replace(/\s/g, ‘’); // Removes all spaces
let result = str.replace(/l/g, ‘’); // Removes all lowercase “l”
how to loop easy over an array?
arr.map((item, index) => {
if (item === target) { indexFounded = index; } })
.map() is meant for transforming arrays (i.e., creating a new array from the existing one).
why .map is not good idea to use it for a loop?
how to re do this? arr.map((item, index) => {
if (item === target) { indexFounded = index; } })
arr.forEach((item, index) => {
if (item === target) {
indexFounded = index;
}
});