JavaScript crash course for C/C++ people
This work is licensed under CC BY-SA 4.0.
Under construction. Feedbacks are welcomed.
General
The syntax is mostly the same as C/C++. You’re going to feel like home.
let
works likeauto
in C++.const
works likeconst auto
in C++.Never use
==
in JavaScript. Always use===
and it works like==
in C/C++.A Number is essentially an IEEE754 double. To hold a >56 bits integer, use BigInt.
Variables passed to functions are passed by value.
Objects
Objects can be used like structs and classes. But they’re essentially a
std::unordered_map<std::variant<T1, T2>, U>
whereT1
is String,T2
is Symbol andU
could be anything.Passing Objects to functions works like passing by reference in C++ or passing a pointer in C. That means you can modify the content of the Object passed to the function. But such a “reference” is indistinguishable from a value. If you use Linux and/or other Unix, the best analogy to describe those “references” is hard links on a filesystem.
Assigning an existing Object to a new value will still create a “reference”. They call it “Shallow Copy”. To mimic the behavior in C/C++, you need to do Deep Copy.