Comparative Programming: JavaScript and Python

Henna Singh
9 min readMay 29, 2024

credits: NearLearn

I started with Diploma in Full Stack Software Development with Code Institute a month ago.

This month, as part of the curriculum, I am doing JavaScript and Python together, so I decided to make notes I can refer back to. I have already known JavaScript, but Python is new for me.

Data Types and Variable Declaration

// Declaration in JavaScript

let firstName = 'James' //Strings
let numInt = 10 //Integers
let numFloat = 20.25 //Float
let isTrue = true //Boolean


console.log(firstName)
..
..
##Declaration in Python

first_name = 'James'
num_int = 10
isTrue = True

print(first_name)
...
...

None, undefined & null

If there is a need to represent a variable with no value, or an empty value in a database that is not necessarily an empty string.

None is used in Python to represent data that is simply empty or non-existent. The equivalent in JavaScript is null and undefined.

JavaScript has another data type called undefined , which represents a piece of data that exists in memory but has not been explicitly defined as having any value.

Arithmetic Operators and Operator Precedence

Operator Precedence, or the order of operations, is a set of rules which governs which procedures to perform first in a given arithmetic expression. PEMDAS is a common acronym which stands for parentheses, exponents, multiplication & division, addition & subtraction.

Operator Precendence (Code Institute)
let num1 = 3
console.log(num1)
let num2 = 4 * 4
console.log(num2)
let num3 = (1 + 2) * 3
console.log(num3)
num1 = 3
print(num1)

num2 = 4 * 4
print(num2)

num3 = (1 + 2) * 3 ## what happens if you remove brackets?
print(num3)

Comparison Operators

Comparison operators, sometimes called relational operators, are used to make comparisons between two different pieces of data and return true or false based on whether the condition is met. Most often, these operators will be used to control the flow of the application. For example, you may want to execute a certain block of code only if some value is greater than another value, or only if two values are equal.

(Referred image from Code Institute)

Logical Operators

Logical operators are a set of operators in programming that allow you to perform logical comparisons and operations. Logical operations involve evaluating the truth values of multiple conditions and arriving at one final truth value. There are three main logical operators : AND, OR and NOT , with NOT having the highest precedence, then AND and finally OR.

The AND operator returns true only if all operands are true:

The OR operator returns true if any of the operands are true:

The NOT operator negates the operand when placed in front of it:

Type Operators

This operator can help determine the type of the data involved in an operation. If there is a block of code, that gives an error on string value, then type operator can be used to confirm that data is not of type string. In Python, the type operator is actually a function, with the same purpose.

Reference from Code Institute course

Bitwise Operators

To a computer, any piece of data can ultimately be broken down into its individual binary bits — a series of ones and zeros. For example, the decimal number 1 in binary notation can be represented as 0001, 2 can be represented as 0010 and 3 can be represented as 0011.

In binary, the first digit (reading right to left) always has a value of 1, the 2nd has a value of 2, the third a value of 4, the fourth a value of 8, and so on. To convert from binary to decimal, you simply add the decimal values of each bit with a 1 together, hence 0011 becomes 0 + 0 + 2 + 1 = 3.

Reference image from Code Institute course

Sometimes you might want to manipulate data on a binary level, such as shifting bits that are “on” to the left or right, swapping bits with one another, or comparing bits together using logical operators like AND, OR or NOT. For these purposes, in most programming languages there exist the bitwise operators below:

From Code Institute Full Stack course

Truthy and Falsy Values

All data will return a true/false value if tested for truth in a boolean context. The "truth" of a piece of data might be whether it is positive or negative, whether it has any content, or whether it exists at all.

To check the “truthiness” or “falsiness” of a value, you can use Boolean(value) in JavaScript and bool(value) in Python.

Equality and Identity

When coding, it’s important to remember that the computer will always treat things 100% literally unless you tell it not to. When checking equality, developers can check not only the equality of the values but also whether the data types are the same, and whether or not the two objects being tested are actually the same object in memory.

Flow Control and Iteration

Conditional logic provides the foundation for the computer’s decision making capabilities.

All conditional logic is based on conditions, which are simply the results of a test in boolean form. In other words, a condition is either true or false. If the condition is true then one block of code will execute, and if it's false then a different block will execute

if some_condition:
code to execute if true
else:
code to execute if false

In JavaScript the syntax is different using the curly braces to mark the start and end of each code block:

if(some_condition) {
code to execute if true
} else {
code to execute if false
}

with conditional logic, the application can be provided with options to handle specific situations.

Example JavaScript code:

let result = true;
let city = 'Dublin';
let firstResponse;
let secondResponse;

if(result) {
firstResponse = 'result is true';
}

if(city) {
secondResponse = 'Thank you for choosing a city';
} else {
secondResponse = 'You need to fill in the name of a City';
}

console.log(firstResponse);
console.log(secondResponse);

Example Python code:

color = 'blue'
primary = True

second_statement = None

# write your if statement here
if(color == 'blue'):
first_statement = 'The color is blue'

if(color == 'red' or primary):
second_statement = 'Either the color is red or primary is True'

# Add your print statements here
print(first_statement)
print(second_statement)

Ternary Conditionals

If the condition is true, x will be set to trueValue and likewise if the condition is false, x will be set to falseValue.

let x = condition ? 'trueValue': 'falseValue'
x = 'value1' if some_condition else 'value2'

If/Else If/Else

This is used when there are more than one choice to make.

if (condition1) {
result1
} else if (condition2) {
result2
} else if (condition3) {
result3
} else {
default result
}

Minor difference in Python for the same

if condition1:
result1
elif condition2:
result2
elif condition3:
result3
else:
default result

There can be as many else if/elif blocks as you want, but for conditions with a lot of choices there is a better option called a switch case which offers an efficient way for multiple conditions.

For Loops

For loops allows to repeat the same block of code a specific, fixed number of times.

Example in JavaScript

for (let i = 1; i <= 10; i++) {
console.log(i);
}

// for (initialization; condition; iteration) {}

Example in Python

for i in range(1, 11):
print(i)

# for Strings

my_string = 'abc'
for letter in my_string:
print(letter)
a
b
c
for i, letter in enumerate(my_string):
print(i, letter)
0 a
1 b
2 c

In Python, the loop uses the range() function, which is built into Python, to print the numbers 1 through 10, the last number is not included in the range but the first one is.

While loops

allows iteration indefinitely until a specified condition is met. They can be used as an alternative to for loops, but there are situations, where one will be more suitable than the other

in JavaScript

let i = 1;
while (i <= 10) {
console.log(i);
i++;
}

in Python

i = 1
while i <= 10:
print(i)
i += 1

A key difference between while loops and for loops is that a while loop requires a boolean condition in order to function. For loops do not have this requirement in all languages, they just need an iterable to iterate through. A while loop doesn’t need an iterable at all. In fact, you can deliberately create an infinite loop with a while loop:

while True:
# execute forever

Break, Continue & Pass

Sometimes you might want to break out of a loop early, when a specific condition is met. To accomplish this, in most languages there are built-in control statements that will allow you to terminate the loop early or skip a specific iteration.

In JavaScript and Python, these can be accomplished with the statements break, continue and pass. Below is a table describing the functionality of each of these statements:

The break, continue and pass statements are used to control the execution of loops at a more granular level. They can be used in any sort of loop. A common use of break is to break out of an infinite while loop such as this one in Python:

while True:
keep_going = input('Would you like to continue? (Y/N) ')
if keep_going == 'N':
print('You entered N. Exiting!')
break
else:
print('You entered Y. We will keep going!')

You might also want to break out of a loop when a specific condition is met, like in this JavaScript for loop which will print the numbers 0 through 4 to the console, but stop before printing 5 because the if statement will be true and the break statement will execute, breaking the loop:

for(let i = 0; i < 10; i++) {
if(i === 5) {
break;
} else {
console.log(i);
}
}

While the break statement breaks out of a loop entirely, the continue statement makes it possible to skip a specific iteration. The following adjustment to the above JavaScript for loop will print the numbers 0 through 9, but skip 5, continuing with 6:

for(let i = 0; i < 10; i++) {
if(i === 5) {
continue;
} else {
console.log(i);
}
}

In Python there is one more statement as well, pass. The pass statement tells the interpreter to do nothing, but continue on with the rest of the code normally. This is slightly different from the continue statement if you consider the following situation:

for i in range(0, 10):
if i == 5:
continue
print(i)

This code will do the same as the above JavaScript loop, printing the numbers 0 through 9, skipping 5. However, if you replace the continue statement with the pass statement, 5 will be printed normally:

for i in range(0, 10):
if i == 5:
pass
print(i)

The reason this happens is that the continue statement, in both JavaScript and Python, completely skips the rest of the loop body, while the pass statement only skips the current code block, but executes the rest of the loop body normally. In the above code, because the print statement is in the loop body and not contained in an else block, using continue will skip it, but using pass will not.

In the next article, I will write on Data Structures, Functions, OOPs and Code Structure for both Python and JavaScript.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Henna Singh
Henna Singh

Written by Henna Singh

Technical Speaker and an aspiring writer with avid addiction to gadgets, meet-up groups, and paper crafts. Currently doing Full Stack Diploma at Code Institute

Responses (2)

Write a response

Outstanding piece, got me thinking

Got a fresh perspective, thanks