30 thoughts on “JavaScript Pro Tips – Code This, NOT That

  1. Really nice tips, the console logs styling was new to me. As for the good practice, I really love it, however the bad practice is sometimes needed if you need to code to work in older browsers (like ie11) or webviews in windows applications.

  2. Although console logging can be helpful, learn to use a debugger (vscode, browser, extensions, etc). They are way more dynamic and easy to use.

  3. const turtle = {
    name: 'Bob',
    species: 'turtle',
    legs: 4,
    shell: true,
    type: 'amphibious',
    meal: 10,
    diet: 'berries'
    }

    // bad — too lengthy
    function feed1(animal) {
    return `Feed ${animal.name} the ${animal.species} ${animal.meal} kilos of ${animal.diet}.`
    }

    // terrible — feed who?
    function feed2({ name, species, meal, diet }) {
    return `Feed ${name} the ${species} ${meal} kilos of ${diet}.`
    }

    // java — too awkward
    function feed3(animal) {
    return "Feed " + animal.name + " the " + animal.species + " " + animal.meal + " kilos of " + animal.diet + "."
    }

    // excellent — readable and concise
    function feed4(animal) {
    const { name, species, meal, diet } = animal
    return `Feed ${name} the ${species} ${meal} kilos of ${diet}.`
    }

    console.log(feed1(turtle))
    console.log(feed2(turtle))
    console.log(feed3(turtle))
    console.log(feed4(turtle))

    > Feed Bob the turtle 10 kilos of berries.
    > Feed Bob the turtle 10 kilos of berries.
    > Feed Bob the turtle 10 kilos of berries.
    > Feed Bob the turtle 10 kilos of berries.

Leave a Reply

Your email address will not be published. Required fields are marked *