Getting started JavaScript as a C++ programmer

Sakib Noman
2 min readFeb 20, 2021

If you are a C++ Programmer then starting with JavaScript is almost easier for you. I will describe the similarity of syntax between these two programming language.

The first program (Syntax overview)

C++:

#include<iostream>
using namespace std;
int main()
{
cout<< "Hello World";
return 0;
}

JavaScript:

console.log("Hello World");

To start learning a programming language you have to learn five important things at first:

  • Data type
  • Condition
  • Loop
  • Array
  • Function

Data type

Data type declaration is much easier in JavaScript than C++. You have to write only the var keyword instead of writing a specific data type name. ( In ES6 let and const keywords are used to declare data type). But you have to keep in mind the types of data:

number type: Similar to int, long int, float, double. Instead of all these data types, you need to keep in mind only number type data. JavaScript will handle other things for you.

var intType = 123;
var floatType = 123.321;

string type: string is similar to C++ string. You don’t need to learn extra things.

var stringType = "This is a String.";

array: array is also similar to C++ array.[Declarion is different]

JavaScript array:

var intArray = [12,23,34];

C++ array:

int array[] = {12,23,34}; 

Condition

There is no difference between the JavaScript condition and the C++ condition. The main concept is similar of course moreover syntax is also similar.

//if else or nested if else
if(num1 > num2)
{
statement...
}
else if(num1 == num2)
{
statement...
}
else
{
statement...
}

Loop

Loop is also the same in these two programming languages.

//for loopfor(initialization ; condition; increment/decrement)
{
statement...
}
//while loopinitialization;
while(condition)
{
statement...
increment/decrement
}

Array

There are some differences in declaring an array. You don’t have to initialize an array with size in a square bracket after the array variable name. JavaScript Dynamically handles this. In JavaScripts, you can keep all kinds of data in the same array such as int, float, string, object, etc.

var intArray = [1,2,3,4];
var floatArray = [1.1,2.2,3.3];
var stringArray = ["Sakib","Rakib","Akib"];
var mixedArray = [1,"Sakib",2,"Akib"]

Function

In the function there is also some difference, you don’t have to declare return type in JavaScript and you can declare a function only using the function keyword. the ES6 version has a more easy function called the arrow function which has made our life easier.

function myFunction()
{
statement...
}
//Arrow Functionvar myArrowFunction = () => statement;

--

--