JavaScript DeepClone function created by ChatGPT and it works

One thing I hate dealing with is cloning JavaScript Objects. Well, deep cloning JavaScript Objects. So I decided to see if AI could write me a quick function.

And it did.

I asked ChatGPT “Can you write me a JavaScript function which will deep clone a JavaScript Object?”

And this was it’s response:

Certainly! You can create a deep clone function in JavaScript using recursion to clone nested objects and arrays. Here’s a sample function to deep clone a JavaScript object:

function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  if (Array.isArray(obj)) {
    const cloneArr = [];
    for (let i = 0; i < obj.length; i++) {
      cloneArr[i] = deepClone(obj[i]);
    }
    return cloneArr;
  }

  const cloneObj = {};
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key]);
    }
  }

  return cloneObj;
}

You can use this deepClone function to create a deep copy of a JavaScript object. Here’s an example of how to use it:

const originalObj = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Exampleville',
  },
};

const clonedObj = deepClone(originalObj);

console.log(clonedObj);

The clonedObj will be a deep copy of the originalObj, so you can modify it without affecting the original object.

Stop browser from filling in form fields with the autocomplete feature

To be helpful, browsers will cache information you enter in a form field. Then when you comeback and start typing, the same information will pop up near the field so you can click it to have it auto-entered for you. While this is useful, there are times you don’t want this behavior — like a one-time PIN number or CVC code for credit card payment.

Thankfully, there is an easy fix. On either the form itself or the specific fields you wish to stop this behavior, you can add the autocomplete property and set the value to “off“.

autocomplete="off"

On the form itself

<form method="post" action="/form" autocomplete="off">
     ...
</form>

or

On just an input field

<form method="post" action="/form">
  ...
  <div>
    <label for="ssn">Social Security Number</label>
    <input type="text" id="ssn" name="ssn" autocomplete="off">
  </div>
</form>

This accomplishes 2 things. It tells the browser not to save the data for use later. And it stops the browser from caching the data in session history.

This will work with most modern browsers….with one exception. Most modern browsers will not comply with the autocomplete=”off” for login fields; whether on the form or a field itself. It has to do with the way browsers handle password saving/creating internally. So just keep that in mind.

Browser compatibility

Data on support for the input-autocomplete-onoff feature across the major browsers from caniuse.com

For more information:
https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

JavaScript — Get the Currently Focused Element

In JavaScript, the currently active element is said to have Focus, which means it’s the element being acted upon. Like a form field in which you are typing is said to have Focus.

You can get the currently focused element with a simple reference on the Dom:

var currentElement = document.activeElement

This will return the element which has focus, or null if there is no focused element. It will let you have access to the whole element. You can get to any properties on that element like the Id or Name. The CodePen below is a simple example of using the document.activeElement

Example

See the Pen Get Focused Element by Chris (@cgreenws) on CodePen.

For more information:
https://www.w3schools.com/jsref/prop_document_activeelement.asp
or
https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement

JavaScript — How to use the Set object

JavaScript has an object called Set which will store any type with unique values — this can be a primitive value type (e.g. string, number, boolean, null, undefined, symbol) or even an object reference.

//create a new Set
var setA = new Set();
//use .add() to add types to the set
setA.add(6); // Set is [6]
setA.add(9); // Set is [6,9]

//or create a Set by passing it an iterable object
var setA = new Set([6,9]); // Set is [6,9]

Because a value in a Set may only occur once, it will eliminate duplicates when you add them.

//Eliminates duplicate values on creation
var setB = new Set([6,7,8,9,6,7]); // Set is [6,7,8,9]

//Doesn't add new values if they already exist in the set
var setB = new Set();
setB.add(6)// Set is [6]
setB.add(7)// Set is [6,7]
setB.add(8)// Set is [6,7,8]
setB.add(6)// Set is [6,7,8]
setB.add(9)// Set is [6,7,8,9]
setB.add(7)// Set is [6,7,8,9]

Use the .has() method to check if a value is in the set. It will return a boolean of True or False depending on if it finds the value.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true
setA.has(2); // false

The .delete() method will remove a value from the set. It will return a boolean value if it found the value in the set.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true
setA.delete(5); // returns true
setA.has(5); // false -- Set is now [6,9,3,8]
setA.delete(5); // returns false

You can also remove ALL the values in the set with the .clear() method.

var setA = new Set([6,9,3,5,8]);
setA.has(5); // true

A Set does not have a Length property but it does has a Size which will tell you how many values are stored in the Set.

var setA = new Set([6,9,3,5,8]);
setA.size; // return 5
setA.length; // returns undefined

To keep the Set similar to the Map object, values are stored as a key/value pair where the key is the same as the value. You can retrieve all the values in a Set using the .entries() method. This will return an Iterator of all entries in a format of [value, value].

var setA = new Set([6,9,3,5,8]);
var myEntries = setA.entries();

You can also just get the keys or values for a Set using the .keys() or .values() methods, respectively. Both return a new Iterator object. Since the value is used as the key in a Set, both methods ultimately return the same result.

var setA = new Set([6,9,3,5,8]);

var setKeys = setA.keys();
// or
var setValues = setA.values();

Because a Set is an iterable object, you can use the .forEach() method to loop over the values in a Set. You can pass a call back function in as a parameter which will get called once for each value in the set. It also has an option argument which can be passed that will be used for the this value for each callback.

var setA = new Set([6,9,3,5,8]);

setA.forEach(function writeValue(value) {
     console.log(value);
});

// 6
// 9
// 3
// 5
// 8

// example passing 'a' as the thisArg to the forEach
setA.forEach(function writeValue(value) {
     console.log(value + '---'+ this );
}, 'a');

// 6---a
// 9---a
// 3---a
// 5---a
// 8---a

If you just want to iterate over the values in a Set, you also have the option of using a For loop.

var setA = new Set([6,9,3,5,8]);

for (let item of setA) {
     console.log(item);
}
// or 
for (let item of setA.values()) {
     console.log(item);
}

// both output
// 6
// 9
// 3
// 5
// 8

You can easily convert from a Set to an array using Array.from() method.

var setA = new Set([6,9,3,5,8]);

var array1 = Array.from(setA); // [6, 9, 3, 5, 8]

//you can also use the Spread Syntax to create the Array of Values
var array2 = [...setA]; // [6, 9, 3, 5, 8]

One neat trick you can use Set for is to de-dupe an Array.

var array1 = [6,9,3,5,8,9,4,2,1,2,2]

var setB = new Set(array1);

var array2 = [...setB] //[6, 9, 3, 5, 8, 4, 2, 1]

// or you can simplify things like so
var array3 = [...new Set([6,9,3,5,8,9,4,2,1,2,2])]; 
//[6, 9, 3, 5, 8, 4, 2, 1]

Another is to use Set as a way to break a string apart to it’s individual unique letters.

var myString = 'Hello';

var charSet = new Set(myString); // ["H", "e", "l", "o"]
charSet.size; //4

//just keep in mind that case sensitivity counts
var myString2 = 'HeLlo';

var charSet2 = new Set(myString2); // ["H", "e", "L", "l", "o"]
charSet2.size; //5

There are a couple things to keep in mind. A set can only contain unique values. When you interate over a Set object, it returns the values in insertion order (how they go in is how they come out).

Objects can be a bit confusing at first compared to primitive types. For instance:

var obj = {'a': 1, 'b':2}
var obj2 = {'a': 1, 'b':2}
var mySet = [{'a': 1, 'b':2}, obj, obj2]

// mySet now contains
// [{'a': 1, 'b':2}, {'a': 1, 'b':2}, {'a': 1, 'b':2}]

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