daily js - every vs some
every returns true only if every item matches the condition
some returns true if atleast one item matches the condition
const numbers = [2, 4, 6];
[2,4,6].every(n => n % 2 === 0); //true
[2, 3, 6].every(n => n % 2 === 0); //false
(1/2)
daily js - includes vs some
some checks if any of the item matches the condition
includes checks if the item is present in the array
const nums = [1, 2, 3];
nums.some(n => n > 2); // true
nums.includes(2); // true
#day29#frontend#dailyjs#javascript
daily js - includes vs find
includes checks if the value is present in the array, returns true or false
find returns the first item matching the condition
const nums = [1, 2, 3, 4];
nums.includs(1) - true
nums.find(n => n>2) - 3
#day28#frontend#dailyjs#javascript
daily js - lazy loading
Loading a piece of code only when it is needed.
example:
home page loads first
profile page loads only when you open it
- faster page load
- smaller initial bundle
#Day25#frontend#dailyjs#javascript
how it works:
commonJS imports files when the code runs
if (true) {
const math = require("./math");
}
CommonJS loads modules at runtime using require() and shares code using module.exports.
#JavaScript#Day24#WebDev#Frontend
Daily JS - CommonJS
CommonJS is a way to share code between files in Node.js
Before browsers supported import/export, Node used CommonJS.
function add(a, b) {
return a + b;
}
module.exports = { add };
const { add } = require("./math");
add(2, 3); // 5
(1/2)
Not 100% accurate - listen to online / offline events for reliability. because, it shows true eventhough wifi doesnot have internet
#WebDev#Day23#Frontend#DailyJS
Daily JS - https://t.co/cCB1nI9998
tells you if the browser is online or offline.
Useful for showing offline UI, disabling actions, or retrying APIs.
(1/2)
Daily JS - Webpack
Webpack is a module bundler
It takes many JS, CSS, and asset files and turns them into files browsers can load.
#JavaScript#WebDev#Frontend#Day22
- sayHello is not inside p1 or p2
- it is added to Person.prototype
- javascript finds it using __proto__
prototype is a property of a function used to share methods across all its objects.
#Day21#webdev#JavaScript#DailyJS#Frontend
This lookup process is called prototype chain
__proto__ is a hidden link that JavaScript uses to find missing properties.
#DailyJS#Day20#JavaScript#WebDev
Daily JS - JSON.stringify()
JS objects can’t be sent over the network or stored directly.
JSON.stringify() converts a JS value into a JSON string so it can be saved or sent.
const user = { name: "Test", age: 27 };
JSON.stringify(user);
// {"name":"Test","age":27}
(1/2)
So setTimeout(0) simply pushes your code to the next macrotask, after the current stack + microtasks are done.
That’s why promises always beat timeouts.
Rule to remember:
Microtasks > setTimeout(0) - every single time
#DailyJS#WebDev
Daily JS - setTimeout(0) Truth
setTimeout(fn, 0) does not run immediately and it does not wait for microtasks.
JavaScript always finishes all microtasks first (Promise.then, queueMicrotask) before touching setTimeout. (1/2)
#DailyJS#Frontend#Day18#WebDev