BlogPost_204
Not too much to my story when it comes to JavaScript this week. I have been spending a majority of my time practicing using different methods. So with that I will get straight to business.
- Can you describe the main difference between a forEach loop and a .map() loop and why you would pick one versus the other?
forEach iterates over each element in an array, but always returns undefined. Map will return a new array, also iterating over each element. The key difference seems to be in the return value. I also found that forEach will allow a callback function to mutate the current array.
2. Describe event bubbling.
It describes events that occur on an element, starting with the inner element, up to it’s parent, then the parents parent and so on. A good example I found was an onclick event, stored inside two other onclick events. If you click on the innermost element, then in order, each event will occur. If you click on the middle element, the top two events will occur. If you click on the outermost parent element, only one event will occur. I haven’t really seen the use case for this yet, but maybe it will be more relevant starting next week.
3.What is the definition of a higher-order function?
A higher order function is a function that takes in another function as an argument, or returns a function. This made me wonder what the difference was between a callback function and the HOF. The callback is the function passed into the argument. Semantics matter.
4.ES6 Template Literals offer a lot of flexibility in generating strings. Can you give an example?
If you want to print a specific message in the console, you can do it with ease. Then there is it’s functionality with placeholders and expressions. When looping over elements in an array, especially a particularly complicated piece of data, rather than having to constantly type out the bracket/ dot notation for your element you want to print out, you can simply print out the expression `${}
`, and it will save time and be much more readable.
5.What Is an associative array in JavaScript?
An array with key value pairs.
6.Why should you never use new Array in JavaScript?
I don't know but I wont use it.