The first one is used for looping through the properties of an object. The second one is used for looping through an iterable container, such as Arrays, Strings, Maps, NodeLists, and more:
Sets in JS
Sets are just arrays that cannot have duplicate values inside of them. Some basic methods are:
Maps in JS
Map - array of key value pairs, keys can be any data type, remembers insertion order of the keys
Datatypes
There is type conversion in JS, and is done either automatically or my casting.
*Regular expressions in JS: **
The syntax is:
In JavaScript, regular expressions are often used with the two string methods: search() and replace().
The simple error catching syntax is the same as c++. We have:
Here is an example:
Here, adddlert is purposely not defined, hence, an error is raised. With throw, we can throw custom errors (strings, numbers, or objects) and control program flow. e.g.
Here is an example of using error catching:
JavaScript has built in error objects. They contain two properties, name and message. There are 6 different values that can be returned by the error name property:
In JS, we have something called hoisting. Essentially, JS has the default behavior of moving all declarations to the top of the current scope (current script or function). This means that we can declare a variable after it has been assigned and used.
So far, we’ve seen a couple ways to declare variables, such as var, let, and const.
let and const are different from var in that they cannot be declared after they are defined. so, the below code snippets would be invalid:
JavaScript only hoists declarations, not initializations. So we cannot have initialization after it is used (the opposite of C++)