JavaScript is one of the most popular programming languages for web programming.
In this article, we’ll look at the basic syntax of modern JavaScript
JSON
We can create JSON strings from JavaScript objects with the JSON.stringify
method:
And we can convert JSON strings back to JavaScript objects with JSON.parse
:
We can use it to store data in local storage.
We’ve to convert objects into strings first to store them.
To store objects we call localStorage.setItem
:
The first argument is the key.
And we can get data by their keys with getItem
:
Loops
avaScript comes with various kinds of loops.
One kind of loop is the for
loop:
We can loop through any kind of iterable object with the for-of
loop:
Some iterable objects include arrays, strings, and node lists.
Another kind of loop is the while
loop:
There’s also the do-while
loop:
The break
keyword lets us end the loop early:
The continue
keyword lets us skip to the next iteration:
Data Types
JavaScript comes with various data types.
They include:
Objects
We can create an object with curly braces:
It has properties and methods.
this
is the student
object itself.
We can call fullName
with student.fullName()
.
And we can assign values to properties with:
student.age = 19;Promises
We can create promises with the Promise
constructor:
We can resolve
to fulfill the promise with the sum.
And we call reject
to reject the promise with an error.
Then we can call then
to get the resolved value and catch
to get the error values:
Conclusion
We can work with JSON, local storage, promises, and loops with JavaScript.