Comparing script languages to C++ is a rare thing, because these languages are very different and serve different purposes. But when I needed to learn Javascript, while already knowing C++, I really needed a simple comparison table for an easier start. I did not find such comparison, so I created it. I hope this comparison table is useful for developers, which already have experience in some of the following languages, and are learning one of these languages.
You can find more details and examples in the following articles:
https://blog.glyphobet.net/essay/2557/
https://www.educba.com/python-vs-javascript/
https://hackr.io/blog/python-vs-javascript
Feature | C++ | Python | Javascript |
---|---|---|---|
Type | statically typed | strongly dynamically typed (variables cannot be implicitly coerced to unrelated types) | weakly dynamically typed (variables can be implicitly coerced to unrelated types) |
Type coercion | Numeric | Numeric, container to bool | Numeric, strings, objects etc. (disabled with ===) |
Immutability | All objects are mutable if const is not used. Const object is immutable if it has no mutable members and const_cast is not used. | Immutable: int, float, complex, string, tuple, frozen set, bytes. Mutable: list, dict, set, byte array. | Immutable: string, number, boolean. Mutable: objects and arrays. |
Variable declarations require initial values | do not require | require | do not require |
Local variable declaration | int x = 1 |
x = 1 |
let x = 1 |
Global variable declaration from local scope | impossible | global x , and then x = 1 |
x = 1 |
Primitive numeric data types | int and uint up to 64 bit. float up to 64 bit. | int with unlimited precision. float up to 64 bit. | int stored as 64 bit float (up to 54 bit without precision loss). |
Nothing | Only with std::optional |
None |
null |
Undefined (uninitialized) variables | Variable can be undefined if not initialized, but it will return random value if used, instead of 'undefined'. | All variables are initialized — so there are no undefined variables. | undefined |
Declarations are hoisted | no | no | hoisted |
Hash tables | std::unordered_map |
dict , defaultdict |
Object and Map |
Array of different-typed elements | No default | list |
array |
Assignment | expression (returns value) | statement (does not return value) | expression (returns value) |
Function declaration | statement (does not return value) | statement (does not return value) | expression (returns value) |
Lambda functions | Yes | Cannot contain statements, only expressions. Do not create their own scope. Cannot raise or catch exceptions, have if blocks or loops. Cannot have multiple return points | Function assignment. Arrow functions. |
Named arguments of function | No | Yes | Using destructuring: function f({x, y} = {x:1, y:2}) {} |
Missing/extra parameters in a function call | Will not compile | Exception | Missing parameters get value of 'undefined'. Extra parameters get into 'arguments' object. |
Generator functions (yield output) | no | yes | yes |
Coroutines (yield output and input) | no | yes | yes |
File scope | Files do not have their own scope, but you can use static and unnamed namespace to create one: namespace { a = 1; } | Files have their own scope | Files do not have their own scope, but you can create separate scope with anonymous function: (function(){ let a=1; })(); or !function(){ let a=1; }(); |
Namespace creation | namespace, class, function, block | function, class, module | function |
Can call constructor or method without instantiating an object | no | C.f(o) (have to pass some object instead of self) |
С.prototype.f(); |
Accessing object members | member |
self.member |
this.member |
Can attach function to an object method | no | obj.f = f.__get__(obj) |
o.func = function() {}; |
Multiple inheritance | yes | yes | no |
Performance | Highest | Lower | Medium |
Concurrent multithreading | Full — can use all processor cores | Limited (threading module is not concurrent if you cannot release GIL with i/o operation or external code, multiprocessing module uses processes instead of threads, Twisted uses event model) | Full (web workers and worker threads) — can use all processor cores |
Комментарии (4)
jknight
18.12.2019 22:41+1Comparing C++/Rust/Go would make a bit of sense. Comparing Javascript to other scripting/web languages (Python/PHP) would make some sense too.
Comparing enterprise languages like Java/C# would make it too.
Comparing SQL dialects from different database servers would be good to read as well.
Comparing functional languages like Haskell/Scala/F# is okay to me as well.
Comparing C/C++/Python/Pascal/Scheme/Scratch/… from the viewpoint of being a first language to learn is great!
Comparing C++/Python/Javascript as they are feels like comparing a cat to a building.
Rualark Автор
19.12.2019 01:06Hi. I think the comparisons you mentioned are more easy to find. But when I needed to compare these three languages, I did not find it, so I created the comparison. Hope that it helps those who need it like me.
VaskivskyiYe
Why these languages? Where at least some details? Or that's just to make people visit your web page?
Rualark Автор
Hi. I added a link for details and answered your question about languages below.
By the way, I did not know that my web page was showing below. Not sure if it is much needed here :)