JavaScript — Loop Over an Array with forEach() Method

You can use the forEach() method to loop over an array and execute a function on each element of the array.

var pets = ['dog', 'cat', 'monkey'];

pets.forEach(function(element) {
  console.log(element);
});

// expected output: "dog"
// expected output: "cat"
// expected output: "monkey"

For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Electron Error: Not allowed to load local resource

I’ve recently begun playing with Electron (the library that lets you build cross-platform desktop apps with web technologies). And not long into trying to hook up my default HTML page I received this error:

Not allowed to load local resource

I was certain I’d typed the line to load the URL just like the tutorial suggested. I searched around and was having trouble finding out why my app wasn’t working. Finally, in a reply buried in the comments was the answer. I was using single quotes and what I needed was the back tick for my value passed.

I had this

mainWindow.loadURL('file://${__dirname}/countdown.html');

But needed this

mainWindow.loadURL(`file://${__dirname}/countdown.html`);

It’s an incredibly easy thing to not notice, but because I was dynamically swapping out the ${__dirname} variable, I had to use the back ticks (`) and not the single quote (‘).

Hopefully that will help other Electron beginners experiencing the same issue.

What is the JavaScript ternary operator?

The ternary operator is a JavaScript Operator which will return one of two values depending on a conditional supplied. Basically, you supply any conditional which will evaluate out to true or false and it will return one of the two expressions. It is set  up as such:

condition ? expression1 : expression2

If the condition evaluates true, then expression1 is returned. If the condition evaluates to false, then expression2 is returned.

Example

var pet = {
    name: "Spot",
    species: "Dog"
};
var soundMakes = pet.species === "Dog" ? "woof" : "meow";
console.log(soundMakes);
//expected output: "woof"

You can use multiple conditions when evaluating. And you can put functions in the expression section to run complex code.

Example

var aDog = true;
var myPet = false;

aDog && myPet ? function() {
    alert("It's my dog");
    console.log('Give the dog a treat');
} ()
:
function () {
    alert("Not my dog");
    console.log('Walk away quickly');
} ();

For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

JavaScript — delete a property from an object

You can remove a property from a JavaScript object with the delete operator.

And it’s super easy:

delete object.property
var Car = {
    make: "Mazda",
    model: "CX-5",
    year: "2014"
};

console.log(Car.year);
// expected output: "2014"

delete Car.year;

console.log(Car.year);
// expected output: undefined

For more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete