February 1, 2025•4 min read••
Tags ▼
No tags
https://javascript.plainenglish.io/11-mysterious-but-interesting-front-end-tips-39f98bc416a9
![]()
Need to determine a user’s network speed? JavaScript’s Network Information API has got you covered! You can access the downlink speed (in Mbps) through navigator.connection.downlink.
if (navigator.connection) { const downlink = navigator.connection.downlink; console.log(`Current download speed: ${downlink} Mbps`); } else { console.log("Network Information API not supported"); }
Keep in mind that browser support for this API can vary.
Want to add a tactile response to your mobile web app? The window.navigator.vibrate API lets you trigger device vibrations.
// Vibrate for 500ms if (navigator.vibrate) { navigator.vibrate(500); } else { console.log("Vibration API not supported"); } // Create a pattern of vibration and pauses if (navigator.vibrate) { navigator.vibrate([200, 100, 200, 100, 200]); }
Remember to check for browser and device compatibility before implementing vibrations.
In specific scenarios, you might want to restrict users from pasting text into input fields. The following snippet shows you how.
<input type="text"></input> <script> const input = document.querySelector('input'); input.addEventListener("paste", function(e){ e.preventDefault() })</script>
Use this with caution! Preventing pasting can hinder user experience in many cases.
No need to rely solely on JavaScript to hide elements. Utilize the native HTML hidden attribute.
<p hidden>I'm invisible!</p>
This has a similar effect to display: none; in CSS, making the element disappear.
inset in CSS 📍Ditch the verbose top, left, right, bottom properties in CSS! The insetproperty streamlines your positioning.
/* Verbose way */ div { position: absolute; top: 0; left: 0; bottom: 0; right: 0; } /* With inset */ div { position: absolute; inset: 0; }
This not only makes your CSS cleaner but also more efficient!
console.log() - Uncommon Console TricksWhile console.log() is a debugging staple, the browser's console offers much more.
console.table(): Displays arrays or objects in a neat table format.const data = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 } ]; console.table(data);
console.group() and console.groupEnd(): Groups related logs for better organization.console.group('Group Label'); console.log('Log message 1'); console.log('Log message 2'); console.groupEnd();
console.time() and console.timeEnd(): Measures the execution time of your code.console.time('myTimer'); // Your code here console.timeEnd('myTimer'); // Outputs the time taken
console.error(), console.warn(), console.assert(): Output errors, warnings, and assertions for more effective debugging.console.clear(): Clears the console.console.dir(): Displays an interactive list of an object's properties.If you need to prevent pull-to-refresh on mobile, the overscroll-behavior-yCSS property is your go-to.
body { overscroll-behavior-y: contain; }
This also helps manage scrolling within modals, preventing the background page from scrolling when the modal reaches its boundaries.
Need a quick way to turn your webpage into an editable area? Use the contentEditable attribute!
document.body.contentEditable = 'true';
Be cautious! This grants users editing power, potentially impacting page structure and script execution. Handle user input carefully and use it judiciously.
Did you know? You can access elements with IDs directly in Javascript, no need for document.getElementById().
While convenient, stick to document.getElementById() for clarity and maintainability in real-world projects.
Enhance user experience with smooth scrolling by adding scroll-behavior: smooth; to your <html> element's CSS.
html { scroll-behavior: smooth; }
:empty CSS SelectorTarget and style empty elements effectively with the :empty selector.
p:empty { display: none; }
This selects and hides empty <p> tags in your HTML.
What are Promises in JavaScript?
Let’s face it: JavaScript can be quirky, but mastering it is deeply rewarding. This article is your treasure chest of…
In the ever-evolving world of web development, it’s easy to get caught up in the allure of the latest JavaScript…
Unlike many other programming languages, JavaScript doesn’t differentiate between integers, short integers, long…
Thank you for being a part of the In Plain English community! Before you go:
Follow on your preferred channel for new articles, notes, and experiments.