Time: 30 minutes
Purpose: This activity starts your journey in learning JavaScript by enabling you to identify specific keywords and patterns that are used in JavaScript code.
Introduction: Reading code and writing code require two very different skillsets, but both help support growth in one another. To get us started with making you feel comfortable with JavaScript syntax we’re going to review some JavaScript code to see what common patterns exist within it before we dive into them and learn how to use them.
Task:
Find a piece of JavaScript code and see if you can locate any of the syntax patterns previously mentioned in the lesson. What do those details tell you about the code you are reviewing?
If the code is publically available, share it on this thread as well as some of the patterns you identified and your thoughts on what the code might be doing.
I had trouble finding some code that was simple enough for me to understand, so I used Mark’s original code from the course.
I can see the variables var, let & const.
var is an older variable from -1995- 2015 and isn’t used, other than on older browsers. In this occasion it is allowing anything called stringVariable to be a string.
Const is a variable that doesn’t change and will always be number 42 in this situation.
On line 15- I can see that the console.log will print ‘I am a function…’
further down on line 39- we have if statements, which say if the colour is yellow, print ‘the colour is yellow’ but if not print red.
Excellent work @kelly.kenyon. Well done for interpretting what the if statement condition is and for noticing how myVar
was called in the function 
Was there anything that was less clear in the code you looked at?
The letMyobject confused me a bit I wasn’t really sure what this was doing.
So myObject
is a JavaScript object that allows us to store more complex data structures. If you’re familiar with JSON, it’s exactly the same principle. An Object can store lots of different data types like strings, ints, arrays and even sub objects.
So for myObject
we have two items in there. Both are strings and they are each assigned a key. So keyOne
is assigned to the string valueOne
. This means if I was to call this Object later in my code like so:
myObject.keyOne
It would return the String valueOne
.
Hope that makes sense.