Semantic Differences Between JavaScript and Python
Differences between two of the most popular programming languages in the world
I’m a full-stack web developer. For the past few days, I’ve been focusing on data structures and algorithms because, for a coder, nothing else matters if you’re not good at problem-solving — every coder should have a strong base in problem-solving.
I started learning Python two days ago and began to understand the language better. I started comparing it with JavaScript because that’s the language that I work in every day.
JavaScript and Python are two very important languages. While many people, including me, know either JavaScript or Python, we’re unaware of the subtle differences between the two languages.
I noticed a lot of semantic differences and I thought that to help myself and others I would contrast a few of the basic concepts (fundamentals) that I’ve come across so far in these two languages. So, if you’re familiar with either JavaScript or Python and want to learn the respective other, you’ve landed in the right spot!
Disclaimer: I am new to Python and code mostly in JavaScript. If I fail to mention certain differences between the languages, it’s probably because I did not reach there.
Let’s start!
Comment
JavaScript
Uses double-slash (//) for single-line comment and (/* */) for multi-line comments.
Python
Uses pound (#) for single-line comment and triple-quotes (“”” “””) for multi-line comments
CODE BLOCK
JavaScript
Every block of code in JavaScript is enclosed within curly braces ({}) and every statement terminates with a semicolon (;)
var a = 2;
if(a>0){
console.log("Positive");
}
else{
console.log("Negative");
}Python
Every block of code in Python uses indentation. It doesn’t use curly braces or parentheses. Instead, whitespace is used for indentation in Python — every statement terminates with a new line.
a = 2
if a>0:
print("Positive")
else:
print("negative")
print("does not belong to else block")Here, the two blocks of code in our example if-else are both indented two spaces. The final print(‘does not belong to else block ’) is not indented, and so it does not belong to the else-block.
Data Types
Discussing differences between data types in JavaScript and Python requires a whole other article. For now, we’ll discuss a few major differences between them.
- The blue table shows simple (or) primitive data types in JavaScript and Python. The
Numberdata type in JavaScript represents bothIntandfloatvalues which will be determined by the compiler at runtime.BigInt(ES 10) in JavaScript is used to store large integer values whereas Python usesIntto represent integers of all sizes. nullkeyword is used in JS to describe an empty value,noneis used in Python.- Python doesn’t have any concepts of
undefinedandsymbols — these are unique to JavaScript. - Python has a special data type known as
complexthat represents complex numbers written in the form, x+yj where x is the real part and y is the imaginary part.
a=3+4j
print(a.real) #3
print(a.imag) #4- The yellow table represents complex (or) non-primitive data types in JavaScript. Lists in Python stores elements of all data types (just like arrays in JavaScript).
- But arrays in Python are defined using libraries such as
NumPy,array, etc. Arrays in Python contain elements of the same data type only. Lists and arrays are quite different from each other in Python. We can perform multiple mathematical operations on arrays rather than on lists. - Objects in JavaScript are similar to a dictionary in Python. Both contain key/value pairs. But objects are the fundamental building blocks of JavaScript whereas a dictionary in Python is merely a container for storing properties.
- A Tuple in Python is a list that’s immutable (cannot be changed). Once the list is described as a tuple, we cannot re-define the values in Python. JavaScript does not hold any concept of Tuple.
- Therefore, Python has built-in hash tables (dictionary) which can be used in hashing. whereas JavaScript does not have any built-in hash tables, methods, or libraries.
Variable Declaration
JavaScript
Declaration of variables in JavaScript is done by using the three main keywords: var, let and const. These declarations determine how the variable can be used (scope).
Python
Python doesn’t use any keywords to declare variables. Instead, it uses variable names to assign a value directly:
a = 3
print (a) # 3If-Else
JavaScript
The conditional statements used in JavaScript are if, else if, else and switch.
Python
The conditional statements used in JavaScript are if, elif, and else.
Get Ruqayyah Sara’s stories in your inbox
Join Medium for free to get updates from this writer.
elif is a shorthand notation for else-if and Python doesn’t contain switch statement. Instead, the dictionary can be used as a replacement for it.
Ternary Operator
JavaScript
Ternary operator(?:) is a conditional operator in JS that operates on 3 operands. The syntax is (condition)?(expressionIfTrue):(expressionIfFalse);
var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"Python
The ternary operator in Python is a conditional operator that follows the syntax, (expressionIfTrue) if (condition) else (expressionIfFalse):
a, b = 10, 20
min = a if a < b else b
print(min)Identity Operators
JavaScript
The identity operator (===) also known as a “strictly equality operator,” compares two operands and checks their equality without coercing them i.e it checks the data type of two operands. It returns a boolean. JavaScript has only one identity operator.
var a=3,b="3";
console.log(a==b); // true
console.log(a===b); // falsePython
Python has two identity operators: is and is not
The is operator evaluates to true if the operands on either side of the operator point to the same object, whereas is not is the converse of is.
x = 5
if (type(x) is int):
print ("true") # true
if ( type(x) is not int):
print ("true")Membership Operators
JavaScript
Membership operators check a specified property in an object. JavaScript has only one membership operator — in.
const user= {name: 'Sara', age: 19, sex: "female"};
console.log('name' in car);
// output: truePython
Membership operators are used for validating the membership of a value. Python has two membership operators: in and not in
x = 24
y = 20
list = [10, 20, 30, 40, 50 ]
if ( x not in list ):
print ("not present") # Output:TrueLoops
JavaScript
There are three loops in JavaScript:
- Entry controlled loops:
forandwhile(test condition evaluated before executing the statements). - Exit controlled loop:
do - while(test condition evaluated after the execution of statements)
// for loop
var c = ["red", "green", "blue", "purple"];
for (var i = 0; i<4; i++){
console.log(c[i]);
}
// prints array// while
var c = ["red", "green", "blue", "purple"];
while(i<4){
console.log(c[i]);
i++;
} // prints array
Python
Two loops in Python — for and while. It does not show any concept of built-in do-while
while loop in Python shows similar behavior to for loop in JS. Considering the above-given example,
c = ["red", "green", "blue", "purple"]
i = 0
while i<4:
print(c[i])
i += 1 # i++ is invalidPython does not support ++ and -- increment and decrement operators. Instead, we can use x+=1 and x-=1 to perform the same operation.
forloop is used to iterate over a sequence (list, array, and tuple). It makes use of in operator and range() function for iterations. We can generate a sequence of numbers using the range() function which accepts three integer arguments: start, stop, and step.
start— tells us to start from which specified position (optional) It will start from 0 by default.stop— tells us to end or stop an integer before the specified position (required).step— specifies the incrementation between the two integers in the sequence (optional).
x = range(5)
for n in x:
print(n) # 1,2,3,4x = range(3,9)
for n in x:
print(n) # 3,4,5,6,7,8x = range(2,20,2)
for n in x:
print(n) # 2,4,6,8,10,12,14,16,18
So these were the few basic differences between JavaScript and Python’s semantics that I noticed. If you feel I’ve missed anything, please don’t hesitate to share it below.










