Since we’ve been using JavaScript for the past several weeks in class and in homework, I thought it would be beneficial to help run through some elements of JavaScript I found weird or difficult at first. I’ve noticed some people have a harder time understanding JavaScript’s syntax or how its variables work, so I wanted to make this as a sort of reference.
Types
JavaScript contains primitive and non-primitive types. A variable can be declared with “var”, “let”, or “const”, more on const later. The main primitive types are1:
- Boolean: true, false
- Null: null
- Undefined
- Number: 72, -174.21
- String: “Hello, ”, ‘world’
For an object type, objects are very loose and can for the most part be defined with relative ease.
Let’s take a simple object, named “car”.
This object has three properties, wheels, year, and color. When declaring or assigning properties, the syntax shifts from the typical use of “var num =”, to “num: ”. These properties in an object are pairs: “wheels” and “4” are a pair, specifically a key-value pair as well as an object. This means that there is more than one way to access an object’s properties.
The second one looks suspiciously similar to a map, specifically one in C++. Objects may be iterated through using a for-loop directly.
Functions
Functions may be defined in a few ways. If inside of an object, the function would be defined as:
Else, it may be defined as the following:
The bottom function declaration is known as arrow notation. A function with arrow notation is not bound to an inherited constructor or the keyword “this”, and its usage with scopes may be troublesome. Its parentheses may be dropped if there does not exist any parameters, such as “getProps => {}”2.
JSDoc
JSDoc is a markup language used to comment JavaScript code, and is similar to JSDoc in many ways. JSDoc may be created by typing “/**”, then pressing enter upon this pop up:
JSDoc is, in my opinion, very useful in declaring classes, functions, objects, types, or declaring types. This is especially important with JavaScript, since types are handled dynamically.
JSDocs can use the following my pressing “@” inside of the comment, revealing a large list of descriptors, with users being able to make any and anytime3:
Upon creating a simple JSDoc annotation:
Hovering over the object “car” will read:
For our function getValue:
This is rendered as:
Hopefully, this post can be helpful. JavaScript was odd for me when I first started, with these topics being some of the things that took me a while to get used to. The sources contain a wealth of information that I have and will continue to refer to and learn from, and hopefully they can help you too.
Links:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures (Types)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions (Arrow Functions)
- https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html (JSDoc)
From the blog CS@Worcester – Chris's CS Blog by Chris and used with permission of the author. All other rights reserved by the author.