4.14 Exercise: Proprieties of an Object

Okay, here's another simple exercise, your job is to create a function called displayProperties. We pass an object in here. And this function needs to display all the properties of this object that are of type string. I'm going to give you an example so at the top I'm going to define an object that we're going to call girl.I'm going to give you an example. So, above, I'm going to define an object, which we'll call movie Here's the literal syntax of the object to initialize it, so here we're going to add some properties like the title, we're going to define it on a simple string, we're going to put for example "Titanic" and year of release we're going to set to 1997. Then we'll add note as a property, once again of type number We'll give it a note of 4.5. Finally director put on another string, we'll call it James. Now, if we call the function displayProperties We pass this movie object to see what we get. We see the channel properties of our movie object, so the title is Titanic and the director is James. So, pause the movie, do the exercise and when you're done, come back and continue watching the fix. All right here, we need to use the for in loop to go through the properties of this object. So we're doing a for let key in object, so on each iteration, this key will contain the name of one of these properties. Let's take a look here, we do a console. log of key . So we get the title, the year of release, etc. Now we need to get the value of each of these properties, and then check the type of that value. If the type is a string, we will display the property and its value. So, to get the value of this property, we use the bracket notation, so key object. Now, to check the type of that value, we use the typeof operator, and then we put that into an if statement. So, if the type of the key object is equal to a string, we simply display the key and the key object. save the changes. We get only the title and the properties of the director. Now you may wonder why I didn't use braces here. Because quite simply we have two lines under this statement, we have only one statement under this For statement, This console. log is a single statement that belongs to our if statement. So here we don't need braces because we're dealing with a single statement under the for statement. That's it for this exercise on the properties of an object we'll see you for a very next demonstration.