All these Accessing Objects and no idea what is going on

All these Accessing Objects and no idea what is going on
0

#1

So these 4 lessons I have done thanks to copy/past. I have gone through the a few times and I still don’t understand their concepts. I dont understand when to use “console.log”, what is the idea of the “[]” and why the examples don’t match up with the answer.

Build JavaScript Objects Complete

Accessing Objects Properties with the Dot Operator Complete

Accessing Objects Properties with Bracket Notation Complete

Accessing Objects Properties with Variables


#2

If you have a specific concept you do not understand, ask the question here on the forum.

The console.log statement is used mainly for debugging purposes. It allows you to check the values of variables at specific points in your code. All it does is displays something to the browser’s console. For example, the following code sums the all the integers between 1 and 10, inclusive.

var sum = 0;
for (var i = 1; i <= 10; i++) {
  sum += i;
}

If I wanted to see the value of the sum variable at each iteration, then I could add a console.log(sum); to the same code (see below).:

var sum = 0;
for (var i = 1; i <= 10; i++) {
  sum += i;
  console.log('current value of sum is ' + sum);
}

When the above code runs, it would display the following to the browser’s console.

1
3
6
10
15
21
28
36
45
55

You can use bracket notation to object’s property surrounded by quotes. If the property name does not have a space in it, then you can use either bracket notations or dot notation. If the property name has a space in it, you MUST use bracket notation. See below where you have to use bracket notation to access the “second Property” property value. (like below):

var myObject = {
  firstProperty: 10,
  "second Property": 20
};

To access myObject’s “second Property”, you must use bracket notation like:

var myObjSecondPropertyVal = myObject["second Property"]; // myObjSecondPropertyVal now contains the value 20

Another time when you MUST use bracket notation to reference an object’s property is if the property name is contained in a variable. See below for an example for how to use a variable (propName) to access the value of myObjects’s “firstProperty” property.

var myObject = {
  firstProperty: 10,
  "second Property": 20
};

var propName = 'firstProperty';
var myObjFirstPropertyVal = myObject[propName]; // myObjFirstPropertyVal now contains the value 10

You will have to give us specific examples of what you are talking about here.