Practical Javascript

This md contains brief pointers on new concepts I encounter as I go through the Practical Javascript course. Click here to see a live version of the app built.

var anArr = [1, 3, 9, 4, 5];

using anArr.splice(2,1) deletes 9 (third item in the array counting from 0).

console.log(anArr); // outputs [1,3,4,5]
var person = {
  name: 'Kizito',
  country: 'Nigeria',
  sayCountry: function () {
    console.log(this.country);
  },
};

person.sayCountry(); // outputs "Nigeria"

The this keyword used in the function within the “person” object refers to the person object (I don’t know if that sentence makes sense). So, just like person.name would output “Kizito”, this.name within a function in the object also outputs “Kizito”.

Javascript Data Types and Comparison.

var obj1 = {
  id: 66,
};

var obj2 = {
  id: 66,
};

console.log(obj1 === obj2); // returns false
var obj1 = {
  id: 66,
};

var obj2 = {
  id: 66,
};

console.log(obj1 === obj2); // returns false

var obj1_1 = obj2;

console.log(obj2 === obj1_1); // returns true

HTML and The DOM

var getAccess = document.getElementById('elementsId');

// Event Listeners can be added so that the HTML element does something when something happens to it:

getAccess.addEventListener('click', function () {
  someAwesomeFunction();
});
var aNumber = 99;
debugger;
if (aNumber > 100) {
  console.log('Gargantuan!');
} else {
  console.log('Minuscule');
}

Important Advice from the Instructor

Gordon, the instructor/author of the course, advices that we, the students, “Focus on understanding and stop worrying about building things from scratch”. This quote might not make sense to you reading this without context, but it does to me. Need context, watch this video from the course.

Refactoring Old Code.

On Functions

var anArr = ['One', 'Two', 'Three'];

anArr.forEach(function (element) {
  console.log(element); // logs every element contained in the array to the console individually
});

On Constructor Functions

function Country(name) {
  this.name = name;
}