Get a Random Array Item with JavaScript

JavaScript Arrays are probably my favorite primitive in JavaScript. You can do all sorts of awesome things with arrays: get unique values, clone them, empty them, etc. What about getting a random value from an array? To get a random item from an array, you can employ Math.random: const arr = [ "one", "two", "three", "four", "tell", "me", "that", "you", "love", "me", "more" ]; const random1 = arr[(Math.floor(Math.random() * (arr.length)))] const random2 = arr[(Math.floor(Math.random() * (arr.length)))] const random3 = arr[(Math.floor(Math.random() * (arr.length)))] const random4 = arr[(Math.floor(Math.random() * (arr.length)))] console.log(random1,... more →
Posted in: JavaScript

Building Table Sorting and Pagination in JavaScript

As part of my job in managing this blog, I check my stats frequently, and I’ve noticed that some of my more basic Vue.js articles have had consistently good traffic for quite some time. As I find myself doing more and more with "regular" JavaScript (sometimes referred to as "Vanilla JavaScript", but I’m not a fan of the term) I thought it would be a good idea to update those old posts for folks who would rather skip using a framework. With that in mind, here is my update to my post from over four years ago, Building Table Sorting and Pagination in Vue.js Raymond Camden… more →
Posted in: JavaScript

JavaScript Class Privates

One of my aspects of JavaScript that drew me to it as a young developers was that its syntax was loose and I could code quickly. As you gain experience as an engineer, you start to realize that some traditional coding structure is a good thing, even if it slows you down. Using Jest or TypeScript to add typing to your JavaScript can save you from maintenance headaches and unexpected errors, for example. While those are pre-compile tools to accomplish structure, we’ve traditionally employed vanilla JavaScript patterns to mock private variables and methods in JavaScript. Did you know, however, that browsers and the JavaScript language support a specific syntax for creating private variables... more →
Posted in: JavaScript

Detect Dark Mode Preference with JavaScript

Seemingly every website, dapp, and app offers a dark mode preference, and thank goodness. Dark mode is especially useful when I’m doing late night coding, or even worse, trading into altcoins. I’m presently working on implementing a dark theme on MetaMask and it got me to thinking: is there a way we can default to dark mode if the user’s operating system also defaults to dark mode? You can determine if the user’s operating system prefers dark mode with one quick line of code: const prefersDarkMode = window.matchMedia("(prefers-color-scheme:dark)").matches; // true This code snippet takes advantage of the CSS prefers-color-scheme media query with JavaScript’s... more →
Posted in: JavaScript
1 2 3 4 5 6 52