Skip to content

C++ vs Python vs JavaScript

Basic Syntax

FeatureC++PythonJavaScript
Case Sensitivity✅ Case-Sensitive✅ Case-Sensitive✅ Case-Sensitive
Blocks & Statements{ } and ; for multiple linesIndentation and newline{ } and ; for multiple lines
Comments// (single-line), /* */ (multi)# (single-line)// (single-line), /* */ (multi)
Boolean Valuestrue, falseTrue, Falsetrue, false
Data Type Declarationint, char, float, etc.No explicit type (e.g., a = 2)let, const, var (e.g., var a = 2)
Characterschar a = 'A';"A" or 'A' (strings used)"A" or 'A' (strings used)
Newline\n, endl\n\n
Logical AND/OR&&, | |and , or&&, | |
Null Equivalentnullptr, NULLNonenull
Function Declarationint add(int a, int b) {}def add(a, b):function add(a, b) {}
Arraysint arr[] = {1, 2, 3, 4};arr = [1, 2, 3, 4]var arr = [1, 2, 3, 4];
Print Outputcout << "Hello" << " " << 2;print("Hello", 2)console.log("Hello", 2);
StringMutable, needs <string>Immutable, built-inImmutable, built-in

and/or

  • In Python, only the keywords and and or are used for logical AND/OR.
  • In C++ and JavaScript, only the symbols && and || are used for logical AND/OR, but in C++, the keywords and and or also work if #include <iso646.h> is added.

print

  • Javascript & Python : Both print() in Python and console.log() in JavaScript automatically add whitespace between comma-separated values when printing.
  • Python: print() automatically adds a newline (\n) after printing. use end="" to prevent new line, print("Hello", end="")

Comments

  • Python: Python does not have a built-in syntax for multi-line comments, but Triple Quotes (''' or """) can be used for multi-line strings, if comment is not assigned to a variable.
'''
This is a multi-line comment.
You can write multiple lines here.
'''
print("Hello, World!")

Variable Declaration

C++JavaScriptPython
std::string s = "Hello";let s = "Hello";s = "Hello"
char c = 'A';let c = 'A';c = 'A'
int n = 5;let n = 5;n = 5
const int n = 5;const n = 5;n = 5 (use final in advanced cases)
auto x = 3.14;let x = 3.14;x = 3.14
const char* s = "Hi";const s = "Hi";s = "Hi"
int a, b = 0; ✅ (a=garbage)a, b = 0 (a = b = 2 ) ✅let a, b = 0; ✅ (a=undefined)
int a, b = 1, 2; ❌ Invalida, b = 1, 2int a, b = 1, 2; ❌ Invalid

Data types

  • C++: Requires explicit data types (int, char, string). auto for type inference.
  • JavaScript: Dynamic typing using let, const, and var (deprecated).
  • Python: Dynamic typing, no explicit type declaration.

Constant

  • C++: Use const for constant variables.
  • JavaScript: Use const to declare immutable variables.
  • Python: No native constant, but conventionally, uppercase names (PI = 3.14) indicate constants. "" vs ''
  • JavaScript & Python: No distinction between char and string—both use ' ' or " ".

a,b=2

  • Python a, b = 2 is valid in Python, but it assigns 2 only to b, while a becomes 2 as well due to implicit unpacking.

Loops

C++PythonJavaScript
for(int i = 0; i < 5; i++)for i in range(5):for (let i = 0; i < 5; i++)
for(int i = 1; i < 5; i++)for i in range(1, 5):for (let i = 1; i < 5; i++)
for(int i = 1; i < 5; i += 2)for i in range(1, 5, 2):for (let i = 1; i < 5; i += 2)
for(auto i : str)for i in "string":for (let i of "string")
for(auto i : arr)for i in (1,6,9):for (let i of [1,6,9])
  • C++/JavaScript: Uses curly braces {} for multi-line blocks.

Conditionals

PythonC++JavaScript
if x > 1: return 1if (x > 1) return 1;if (x > 1) return 1;
elif x > 2:else if (x > 2)else if (x > 2)
else:elseelse
string result = (x == 1) ? "1" : (x == 2) ? "2" : "Invalid";result = "1" if x == 1 else "2" if x == 2 else "Invalid"let result = (x === 1) ? "1" : (x === 2) ? "2" : "Invalid";
  • C++/JavaScript: Uses curly braces {} for multi-line blocks.
  • Python: Parenthesis () is Optional (if x > 1: works fine).
  • Python: No native switch statement (use if-elif-else).

Switch

  • Python use match-case in 3.10+
x = 2
match x:
case 1:
print("One")
case 2:
print("Two")
case _:
print("Invalid") # Default case
# Output: Two
  • C++/JavaScript: Supports switch for multi-branch conditions
// Javascript // C++
let x = 2; // int x = 2
switch (x) {
case 1:
console.log("1"); // cout << "1" << endl;
break;
case 2:
console.log("2"); // cout << "2" << endl;
break;
case 3:
console.log("3"); // cout << "3" << endl;
break;
default:
console.log("inval"); // cout << "inval" << endl;
}
// Output: Two

Data Structures

C++PythonJavaScript
std::vector<int> v = {1, 2, 3};myList = [1, 2, 3]let myList = [1, 2, 3];
int arr[] = {1, 2, 3};myArray = [1, 2, 3]const myArray = [1, 2, 3];
std::map<string, string> my_map = { {"k1", "v1"}, {"k2", "v2"} };myDict = {'k1': 'v1', 'k2': 'v2'}let myObj = { 'k1': 'v1', 'k2': 'v2' };
const int arr[] = {1, 2, 3};myTuple = (1, 2, 3)const myTuple = Object.freeze([1, 2, 3]);
  • C++: Use std::vector for dynamic arrays (preferred for modern C++). Use int arr[] for fixed-size arrays.
  • Python & JavaScript : Lists or Array Use same Syntax ([]) and are dynamic and mutable.
  • Javascript and C++: No native tuples like Python, but const(for both) and Object.freeze()(for JS) provides immutability. ⭐
  • C++: std::map is an ordered associative container (like a dictionary) and but std::unordered_map is unordered
  • JavaScript: Objects ({}) are similar to python dictionaries but unordered by default. Use Map for better handling of key-value pairs.

Comments

C++JavaScriptPython
// Single-line// Single-line# Single-line
/* Multi-line *//* Multi-line */""" Multi-line """
  • Javascript and C++ : Same syntax for comments

Boolean and Falsy Values Comparison

C++JavaScriptPython
Boolean Values
true, falsetrue, falseTrue, False
Falsy Values
000
NULL (or nullptr)nullNone
falsefalseFalse
"" (Empty String)"" (Empty String)"" (Empty String)
{} (Empty Container)[] (Empty Array)[] (Empty List)
N/A{} (Empty Object){} (Empty Dictionary)
N/AundefinedN/A
N/ANaN (Not-a-Number)N/A
  • Falsy values evaluate to False in condition checks.
  • C++ doesn’t have as many implicit falsy values—only 0, NULL, and false are considered false.
  • JavaScript has additional falsy values like undefined and NaN.

🟢 Similarities in functionality

FeatureC++PythonJavaScript
Control Structuresif, for, whileif, for, whileif, for, while
Object-OrientedYes (with classes)Yes (with classes)Yes (via prototypes & classes)
Modules#include (Headers)importimport/export (ES6)
FunctionsYes (with overloading) ⭐Yes (first-class)Yes (first-class)
Loops & Iterationfor, whilefor, whilefor, while, forEach
Error Handlingtry-catchtry-excepttry-catch (async error handling)
Data StructuresArrays, VectorsLists, Dicts ⭐Arrays, Objects
Boolean Valuestrue, falseTrue, Falsetrue, false
Lambda FunctionsYes ([=](int x){return x;})Yes (lambda x: x+1)Yes ((x) => x + 1)
  • 🔴 Differences in Functionality
FeatureC++PythonJavaScript
CompilationCompiled (to machine code) ⭐Interpreted (Bytecode)Interpreted (JIT)
Typing SystemStatic (compile-time) ⭐Dynamic (runtime)Dynamic (runtime)
VariablesStrongly typed ⭐Dynamically typedDynamically typed
Memory ManagementManual (with new/delete) ⭐Automatic (Garbage collection)Automatic (Garbage collection)
PerformanceFast (close to hardware) ⭐Slower than C++Slower than C++ (Browser-optimized)
PointersYes (direct memory access) ⭐NoNo
Syntax StyleCurly braces {}Indentation (whitespace) ⭐Curly braces {}
Main Entry Pointint main()if __name__ == "__main__"No strict entry point ⭐
Use CaseSystem-level, performance appsData Science, AutomationWeb development (Frontend + Backend)
InheritanceMultiple & Virtual InheritanceSingle & Multiple InheritancePrototype-based Inheritance
ConcurrencyThreads (std::thread)GIL (Global Interpreter Lock)Asynchronous (Event loop, Promises)