Difference between Javascript and C++ (Dynamically vs statically typed)

This article is second part of C++ for Javascript Developers series. 📣

Assuming you are an experienced developer either with a CS degree or equivalent experience, please threat this article as a quick reminder of the things that you’ve learned either during your university studies or professionally but haven’t done any for some time because you were, just like myself, mostly developing for the web platform.

Also, the article will be expressed though us “Javascript developers” even though many, if not most, along with myself, started with C like languages but haven’t really had chance to work with them past couple of years.

As JavaScript developers, we never had to go through a hustle of worrying about memory. Having a garbage collector to do that completely detached us from even thinking about it. Sure, we have to worry about memory leaks but most of the things are automated by modern frameworks today. Even variable collisions which used to be a thing few years ago are solved today by encapsulation (modules and Webpack) so even global variables aren’t that much of an issue anymore. We can declare values, use them and let the garbage collector free up the memory when they are no longer needed.

Besides that, like any other dynamic language, we don’t really worry about types. We can use var, let or const followed by any value type, primitive or reference. Also, we don’t have to worry about coercion as this is something done for us by the compiler. Casting as well is not much different. While we do have methods like parseInt, parseFloat, toString and similar, we would use those very seldom at times where you for instance want to convert “5” to a number of 5. The point is, you can do all sorts of crazy stuff with types and completely get away with it. Types and memory allocation are clearly not something that pictures JavaScript as a language and that’s more or less the trait of any other dynamic language out there.

C++ on the other hand is completely opposite. Programming in C++ makes you work directly with memory and due to its static nature, a lot of times you will need to think about which type to use to represent your data, from size to performance perspective. This more or less applies to any programming language that doesn’t have a garbage collector.

This of course can be extremely painful at times but also very satisfying as you have full control over what you’re doing. That basically means that you can build either super efficient software or extremely horrible one.

Not to get you to the point of boredom, I’ve created a table of most important differences between C++ and JavaScript. In case you are unaware of the meaning of some, I urge you to stop reading and research a bit. It’s incredibly important to prepare yourself for the C++ journey and have a big picture about what you are learning before you blindly start writing some code without any meaningful ideas or reasoning.

C++ Javascript
Programming Language Scripting Language
Dynamically typed Statically typed
Low Level High level
System Level Programming (performance intensive apps) Web Development
Compiled Interpreted
Manual memory management Automatic memory management (Garbage collector)
STL npm

We can clearly see that the two languages we are comparing are fundamentally different. Now, that doesn’t mean that they don’t share any traits, they absolutely do. When Brendan Eich created JavaScript, he took most of idea from C programming language because at that time, he was working as a C developer.

For that reason, you will find C++ syntax incredibly familiar and many core language features like statements, loops, functions etc. are gonna look exactly the same as in JavaScript.

In this article, we mentioned some terms that would be incredibly useful for you to understand if you don’t already.

  • Statically typed vs Dinamically typed languages
  • Garbage collection
  • Compiled & Interpreted
  • Difference between programming and scripting languages
  • Memory management

While we will cover all this stuff in the upcoming articles, make sure you do some extra research on top of this article and make sure everything clicks. If there’s something you need clarification for, feel free to ask in the comment section, I’ll be happy to help.

In the next article, we will talk about importance of learning C++, give you some motivation and some insights on why would you want to invest your time learning it and after that, start with articles where we are actually coding stuff.