Zeyad Etman

Thoughts about software development and Life

  • Home
  • About
  • Gallery
  • Twitter
  • Github
  • Books
  • Notes
  • Another way to write your JavaScript


    3 min read


    Hi all, in this post I'll share with you some frontend code, that we can write it in another way, And everything is working well, doesn't break the rules or putting smells in code, is cool.

    1. Generate an array of sequential numbers [1, 2, 3, ...., n]

    If we want to generate an array like this [1, 2, 3, 4, 5, 6, ...., n], We can write code using new Array() with Array.fill() so it'll be

    1const N = 10;
    2new Array(N).fill().map((_, indx) => indx + 1); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    Why new Array(N).map() doesn't work?

    Cool, But if we're working on a large array of sequential numbers, Is this method will be the best? Mmmm, No! because new Array() creates a holey array which is slow compared to packed arrays. So we can avoid this and re-write this method using Array.from() So the code will be

    1const N = 10;
    2Array.from({ length: N }, (_, indx) => indx + 1); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    new array()

    source: https://slidr.io/mathiasbynens/v8-internals-for-javascript-developers#102

    slide: 102

    you can check the holey array in your Chrome Console, so if we write this new Array(10) your console will display [empty × 10] which is an array of empty values.

    More Resources:

    1. https://v8.dev/blog/elements-kinds
    2. https://stackoverflow.com/questions/3746725/how-to-create-an-array-containing-1-n

    2. Number formatting

    Sometimes you want to write a money with specific currency EGP 1000 or a size of something 50 kB one of the ways to write it, simply const money = '1000 EGP'. But there's a nicer way to write formatted numbers using Intl.NumberFormat. So this strings will be

    1const money = new Intl.NumberFormat("en", {
    2 style: "currency",
    3 currency: "EGP",
    4 useGrouping: false,
    5 maximumSignificantDigits: 1,
    7// "EGP 1000"
    9const storage = new Intl.NumberFormat("en", {
    10 style: "unit",
    11 unit: "kilobyte",
    13// "50 kB"

    Note: style units works on chrome 77+, so you can use babel to compile it.

    This is so cool, if you're working on multiple locale and want to switch between them in a better and fully customized way. More info from V8 Blog: Intl.NumberFormat

    3. Styling NonInteracitve elements on focus

    You can't do this using css/html without tabindex and according to MDN:

    Avoid using the tabindex attribute in conjunction with non-interactive content to make something intended to be interactive focusable by keyboard input. An example of this would be using an <div> element to describe a button, instead of the <button> element.

    and w3 says:

    The content should be semantically described using interactive elements (<a>, <button>, <details>, <input>, <select>, <textarea>, etc.) instead.

    So the best practice for this is using addEventListener() in JavaScript, But if you want to use tabindex don't forget to add tabindex to inner html content.

    Another solution

    You don't have to use tabindex if you just want to change the div border. you can use :focus-within and just change the border.

    1<div class="search-box">
    2 <Row>
    3 <div class="search-box-icon"></div>
    4 <input class="search-input" placeholder="search in listbox" />
    5 </Row>
    1.search-box {
    2 margin-left: 1%;
    3 outline: red;
    4 border: 1px solid #fc3;
    7.search-input {
    8 border: none;
    11.search-input:focus {
    12 outline: none;
    15.search-box:focus-within {
    16 border: 2px solid #53c9fc;

    I published this as an answer on stackoverflow

    Finally, I believe that everyone of us has a style writing code, his favorite practices that they don't break the rules, or putting smells in code.

    Signature of the author