Am I going through good practice with JavaScript thus far?

Am I going through good practice with JavaScript thus far?
0.0 0

#1

I’ve been going through the process of working smarter with better understanding of JavaScript Arrays, via .js file extension from .html file and reading via Chrome (and other browser) Console. How is my code practice in this perspective. This is code I copied from MDN with writing it over to get a better understanding with Arrays in JavaScript as I’m in my studies and I’m wondering if I’m heading towards good/great practice with writing code at this level.

I understand that console.log is mainly for practical perspectives. Here a portion of the code:

// Access (index into) an Array item
console.log(“");
console.log("// Access (index into) an Array item");
console.log("
”);
var first = fruits[0];
console.log(first);
console.log(fruits);

var last = fruits[fruits.length - 1];
console.log(last);
console.log(fruits);

console.log(" “);
console.log(” “);
console.log(” ");

// Loop over an Array
console.log(“");
console.log("// Loop over an Array");
console.log("
”);
fruits.forEach(function(item,index,array) {
console.log(item, index);
});
console.log(fruits);

console.log(" “);
console.log(” “);
console.log(” ");

// Add to the end of an Array
console.log(“");
console.log("// Add to the end of an Array using PUSH");
console.log("
”);
console.log("// Add to the end of an Array using PUSH");
var newLength = fruits.push(“Orange”);
console.log(newLength);
console.log(fruits);

console.log(" “);
console.log(” “);
console.log(” ");

This code is coming from MDN, here is the link to their page on learning Arrays:


#2

Good practice, but there are some things you can posssibly do better on. Here’s some ideas.


Redundant. Any programmer who looks at your code knows that fruits[0] is the first item in the array. There is no need to assign it to a variable, just do console.log(fruits[0]). Also, what is it first of? What if you have multiple arrays or such? I think it is better to just use it without a variable.


First off, you have an unused parameter array in the callback. You are not using it, so there is absolutely no need for you to have it in there. Second, it would be better possibly to not use an anonymous function there. If for some reason you must use an anonymous function there, you might as well use an arrow function instead. Also, array#forEach() is a little bit outdated for what you are doing, although it does have some specific uses. The ES6 for...of is considered better in multiple ways several of which are performance and the ability to break or return. Example usage:

for (let fruit of fruits) {
  console.log(fruit)
}

if you absolutely need the index (which I doubt you do), you can get it like this:

for (let [index, item] of array.entries()) {
  // ...
}

You are right that console.log() is more for practical purposes. If you are really trying to test the values of arrays, I think it would be better to use a testing framework.


#3

Hello @IsaacAbrahamson, thanks for reply. Again though, the actual code is coming from MDN and I was wondering more about laying out the way I layed it out for visual on Sublime or even Brackets. I was going about this to see the output for the examples given within MDN and if I was going about it in the right way as I’ve gone into the practical side of my studies.

So ultimately I was going about finding out if this is practiced by others in that way.

As for the unused parameter array, again, it was an example via MDN. And as you mentioned that I didn’t use it, I actually did try using it to see the output and took it back out to the original example, for testing purpose to see the output.

Thank you for your thoughts and I will look further into the difference between the forEach() vs. for…of, as well as the use of a testing framework when I reach that destination. I’m still in front-end mode and have not used a JavaScript framework as of yet as I’m going through my journey within front-end to reaching the back-end with Web Development with Javascript.