Lodash, Day.js in Browser Console: A Debugging Shortcut

Ever wondered why you can use “_” or “dayjs” directly in the console? If you’ve ever visited the documentation for Lodash, Day.js, or Moment.js, you might have noticed a cool "Easter egg": you can open your DevTools console and start coding with the library immediately. No npm install, no import, no setup. It just works. 🤯 How does it work? It’s simpler than you think! These sites intentionally attach their main instance to the Global Window Object. In their source code, they do something like this: window._ = lodash; window.dayjs = dayjs; Because the browser console operates within the scope of the “window”, those variables become globally accessible commands. How can you use this? 1. Debugging your own apps: Stop setting breakpoints just to see a variable. In your dev environment, temporarily use window.myData = data; to poke around the object live in the console. 2. Testing on any site: Want to use Lodash on a site that doesn't have it? Paste this into your console to "inject" it: const s = document.createElement('script'); s.src = 'https://lnkd.in/gQu7368J'; document.head.appendChild(s); 3. The “Pro” Shortcut: Use the Console Importer extension to simply type $i('lodash') on any page. ⚠️ A word of caution While exposing variables to “window” is great for documentation and debugging, keep your production environment clean! Global variables can lead to namespace collisions and security leaks. Keep it for the playground, not the ship. #WebDevelopment #JavaScript #CodingTips #Frontend #Programming #DevTools

To view or add a comment, sign in

Explore content categories