C++ vs JavaScript
The beautiful compensations of this life is that no one can sincerely try to help another without helping himself ~ Ralph Wardo Emerson.
You know, this quote speaks volume of many programming languages as we find them today, JavaScript in particular. JavaScript in the past has been one of the most abused languages but today, it’s one of the most celebrated and even the language with the largest developer community.
Most of these now old and “archaic” languages have once enjoyed popularity in the past. This means that once upon a time the obsolete Basic, Paschal, FORTRAN e.t.c all enjoyed patronage from the developer community. But they failed to evolve as technology grew. So basically one constant thing in programming language evolution is the desire to meet the needs of the users of those languages and also making users developments easier.
In this article I’m going to share with you briefly on some of the difference and similarities I have observed between C++ and Javascript, I know some will say, is it not obvious and clear that they are different and are meant for different things entirely, one is used for writing web scripts while the older is used in building systems applications and all that, yes you are right but isn’t it all good to outline clearly some of the areas they seem to be different and similar? I bet you’ll say let’s go ahead.
So without much ado I will like to start with the differences which might be obvious to all.
DIFFERENCES
- Major use case:
JavaScript was developed majorly to give developers the opportunity to develop programs that will run with the browser efficiently and effectively. It is a dynamic and weakly typed programming language originally created to serve as a scripting language for the Netscape browser. It is an interpreted language, meaning that the code gets turned into machine code as it is being run using a JavaScript engine. Chrome has V8 and Firefox has spidermonkey engine. Lately it has become popular in use on servers (Nodejs) and desktop applications (Electron) Including on cross-platform mobile applications (React and ionic).
C++ on the other hand was built out of C, and it’s regarded as super C or C with objects or C + 1 (in programming the increment operator is ++) and hence the name. C++ is used for programming “bare metal” — meaning that there is no interpreter or virtual machine causing overhead as the code runs. Hence some people call it a low level language. This is why it is often used for writing the core parts of an operating system and for making performance heavy applications — Like games. Most computer games you find are compiled from C/C++ because all those graphics and game logic run better without some interpreter or Virtual Machine (VM, Java and some other languages use JVM). C++ is a compiled language which means that the code has to be built for a particular system.
2. Compilation process:
JavaScript is an interpreted language which means the code gets turned into machine code once it’s run. It’s very generic also which means a JavaScript program can run on virtually any system, this is one reason why it’s been used in building cross-platform and browser applications.
C++ is a compiled language, which means when it’s code is run, the compiler compiles the code directly to machine codes without any bridge (VM) whatsoever. It’s not so generic also, which means the codes are mostly specific to the platform or system it was developed for.
3. Syntactic difference:
To declare variables in JavaScript you don’t need to specify the variable’s data type, you use var (ES5) Or let (ES6) then to declare a variable as a constant you use the keyword const, also it uses garbage collection in order to discharge unused memory. In C++, the story is totally different as you have to ensure you use the correct data type when declare any variable, though you also use const to signify to the compiler that the variable you’re declaring is a constant. In C++ you tend to be in charge of virtually all the memory management (memory allocation and deallocation) using ‘new’ and ‘delete’ pointer for memory allocation and deallocation respectively.
4. Others:
- JavaScript is known to work well with Cassandra and LevelDB including MongoDB, while most common C++ db is PostgreSQL and MYSQL.
- JavaScript supports JVM (Java Virtual Machine) while C++ does not
- JavaScript does not support static types (variables) but C++ does
- JavaScript is portable while C++ is humongous (large sized).
SIMILARITIES
- The first similarity I will point out is that they are both programming languages (pun intended) 😜… not so funny right? I know.
- Both JavaScript and C++ support the object orientation paradigm.
In JavaScript (ES6) classes are declared the following way:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
And class functions are added the following way:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}const square = new Rectangle(10, 10);console.log(square.area); // 100
While C++ has a somewhat complex or not complex (depending on how you see it) way of class creation. In C++, when one creates a class (which is the only way to begin a C++ program), he/she gets both the .cpp and .h files of the class
The CppClass.h
#ifndef CPPCLASS_H#define CPPCLASS_Hclass CppClass{public:CppClass();~CppClass();void setHeight(int);int getHeight() const;void setWidth(int);int getWidth() const;protected:private:int height;int width;};#endif // CPPCLASS_H
The CppClass.cpp
#include “CppClass.h”CppClass::CppClass(){//ctor}CppClass::~CppClass(){//dtor}void CppClass::setHeight(int mHeight) {height = mHeight;}void CppClass::setWidth(int mWidth) {width = mWidth;}int CppClass::getHeight() const {return height;}int CppClass::getWidth() const {return width;}
Both also support class inheritance.
In Javascript:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ‘ makes a noise.’);
}
}class Dog extends Animal {
constructor(name) {
super(name); // call the super class constructor and pass in the name parameter
}speak() {
console.log(this.name + ‘ barks.’);
}
}let d = new Dog(‘Mitzie’);
d.speak(); // Mitzie barks.
In C++:
#ifndef CPPCLASS_H#define CPPCLASS_Hclass CppClass: public MyBaseClass{public:CppClass();~CppClass();void setHeight(int);int getHeight() const;void setWidth(int);int getWidth() const;protected:private:int height;int width;};#endif // CPPCLASS_H
3. Object Destructuring:
Destructuring is an expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
In JavaScript it is represented the following way:
var x = [1, 2, 3, 4, 5];
var [y, z] = x;
console.log(y); // 1
console.log(z); // 2
In C++ it is called Structured binding, and it is usually used both in tuples and arrays. But, I’ll be giving only the example of it’s usage in arrays:
double myArray[3] = { 1.0, 2.0, 3.0 };auto [a, b, c] = myArray;std::cout<<a;
4. Lambda Expressions and functions:
Lambda functions are one feature that is currently trending in most programming languages today. Old and modern languages alike are beginning to introduce lambda functions to simplify development for their users.
In JavaScript, a lambda function is represented thus:
let multiplier = (value1, value2) => return value1 * value2;
In C++, a lambda function is represented thus:
auto multiplier = [] (int first, int second){
return first * second;
};
5. Constant value (variable):
Constants refer to fixed values that the program cannot alter and they are referred to as literals.
In JavaScript:
const name = “Chrisantus Eze”;
In C++:
int const age = 35;
6. Variable number of arguments:
Both JavaScript and C++ have mechanisms that allow it accept variable number of arguments in a function.
This implementation is however different both in JavaScript and C++.
In JavaScript:
function sum(…args) {return args.reduce((previous, current) => {return previous + current;});}
In C++:
void printNames(const char* name…);
7. Ability to declare variables using a non-specific type:
Most modern languages like JavaScript and Kotlin do not use specific data types during variable declarations. Some keywords enable users use any value on the variable declared.
For example in Kotlin “var” is used as a keyword to declare a mutable variable, while “val” is used to declare an immutable variable. This variable can contain any value (string, char, int, double, boolean e.t.c). C++ users got a sense of relieve after the release of C++14 as they can now join their counterparts who work with some of these modern languages in working at such ease without bothering about the data type of the variable before declaring it.
In Javascript:
let name = “Chrisantus Eze”;
In C++:
auto name = “Chrisantus Eze”;
Though the two languages may seem totally dissimilar, but I believe you can now see the few similarities I was able to outline that abound in the both languages.
For further reading on the strengths and weaknesses of the two languages, you can take a look at this article.
Thanks for reading