Key differences between python and JavaScript
Introduction
Python and JavaScript are important languages that are used in web development. Python is a language that can be used in the backend development of a web application. JavaScript on the other hand is a language that can be used in both the frontend and backend development of a web application.
However, despite the difference in development part that each language plays, the two languages have other big differences that we are going to look into detail in this article.
Real World application of Python and JavaScript.
Python is mostly used in scientific and specialized applications as it is also used in web development.
JavaScript is widely used in web development.
Syntax, Sentactical and Functional differences between Python and JavaScript
Python and Js have different Syntax.
Python and JavaScript have some key differences in terms of the syntax used in performing various activities.
Code blocks in python and JavaScript
In Python, when several lines of code are indented, then they are said to be of the same code block.
Example
if t > 10:
print (t)
In JavaScript, notwithstanding, wavy supports are utilized rather than space to bunch explanations that have a place with a similar code block.
Let's look at an example.
if (t>10)
{
console.log(t)
}
Definition of variables
When defining a variable in python, a variable name is written followed by an equal (=) sign. Then a value is assigned to the variable. i.e
<variable_name > = value
x = 56
In JavaScript, a keyword var is added before the variable name and a semicolon is used to terminate the statement i.e
var <variable_name> = value;
eg var k=34;
Variable Naming Conventions
In Python, the snake_case naming style is used. The names should be in lowercase and separated by an underscore as shown below.
first_name
In JavaScript, lowerCamelCase is used. The variable name starts with a lowercase letter and every other new word starts with an uppercase letter.
myFirstName
Constants in Python and JavaScript.
Constants
In Python, constants are written in uppercase format separated by an underscore. ie. CONSTANT_NAME.
For example:
PASS_RATE = 4
In JavaScript, a keyword const is added before the constant name and a semicolon is used to terminate the statement. ie. const CONSTANT_NAME = value;
const AGE=56;
Data types and values in Python and JavaScript
1) Numeric data types
In python, we have three numeric sorts that will assist us with accomplishing exact estimations for logical purposes. These are :
In JavaScript, we have two numeric types i.e Numbersand BigInt. The two whole numbers and coasting point numbers are viewed as type numbers.
None and null in python and JavaScript
In python, when a variable doesn't have a worth at a specific point in the program, it is alloted an extraordinary worth called None.
In JavaScript, the same worth is null which addresses the shortfall of any item esteem.
The undefined value
In python, one needs to allocate an underlying worth to the variable. One cannot declare a variable without an initial value.
In Javascript, we have a unique worth that is allocated consequently. When a variable is declared without assigning an initial value, it prints out undefined as shown below;
var k; // should print undefined
Primitive data types
Python have four primitive data types:
- Intergers.
- float.
- boolean (https://www.codesansar.com/python-programming/fundamental-or-primitive-data-types.htm#bool).
- strings.
On the other hand, JavaScript have six primitative data types:
Comments
Commenting is done for any reason. For example, one may comment on a line for future reference. <!--A commented line is not executed as part of the code. -->
1. Single-line comments
Comment spanning a single line. Look at the example below:
In python, a hashtag (#) is used to comment on a single line.
# this is a single-line comment in python
As shown above we have used a hashtag(#) to comment on a single line in Python.
In JavaScript double slashes (//) are used to comment on a single line.
//this is a single-line comment in JavaScript
As shown above we have used double slashes (//) to comment on a single line in JavaScript.
2. Multiple line comments
These are comments spanning many lines.
When writing multiple comments in python, we start every single line with a hashtag (#) as shown below:
eg
# this is a
# multiple lines
# comment
# as used in Python`
When writing multiple line comments in JavaScript, we use symbols (/*) to open and (*/) to close a comment as shown below:
/*
This is a multi-line comment
that span many lines
*/
Built in data structures
1. Tuples
Tuples in Python are similar to lists but immutable.
They are utilized to store information that ought not be changed
In JavaScript, we don't have an inherent construction with such qualities.
2. List and Arrays-->
Records are utilized in python to store a progression of qualities in a similar information structure
number = [3, 4,5]
An array is the equivalent version of this in JavaScript.
var number = [3,4,5]
Comparing values and types
In Python the double equal (==) operator are used to compare if two values and their data types are equal -->
11 == 11 #True
11 == 10 #False
javascript
As shown above, 11 == 11 is true, while11 != 10 is false.
In JavaScript, a triple equivalent ('===') administrator is utilized to look at if two qualities and their information types are equivalent.
5===5 //true
5==='5' //false
Logical Operators
In python, there are three logical operators:
- and
- or
- not
In JavaScript, the logical operators are also three:
- && -- logical and.
- || -- logical or.
- ! --logical not.
### Type Operators
To check the type of object in python we use the (
type()) function as shown below:
type(instance)
To check the type of object in JavaScript we use type of operator as below:
type of instance
Inputs and Outputs
Input is a function that is used to ask the user of the program a question whereby the user have to type the result of the asked question.
Output on the other hand is used to print the specific message that the user inputs.
Inputs
In python input() function is used to ask for user input.
Example
name = input (" Enter your name : ")
In JavaScript, (if you are running your code on console ) it will display a small prompt with window. prompt (message) and assign the result to a variable.
var input =window.prompt ("enter a Number :")
Outputs
In python we print a worth in the comfort with the print() work parsing the qualities inside the enclosure.
Example
num1 = input ("Enter the first number : ")
num2 = input ("Enter a second number : ")
num3 = input ("Enter a third number : ")
result= num1 + num2 + num3
print(result)
In JavaScript, we print a value in the console with the print() function parsing the values within the parenthesis.
Example
console.log("My name is John:");
Conditional statements
Conditional statements are used to act if a certain condition is true.
1. if statement
In python, indentation is relied upon to indicate the lines of code that belong to a conditional statement.
if condition:
code
Let's look at an example:
age = 10
if age < 18:
print('kid you are!')
In JavaScript, the condition is enclosed in parenthesis and the code enclosed within curly braces which should also be indented as shown in the syntax below:
if (condition)
{
code
}
An example of a snippet would look like:
if (hour > 4) {
task = "Clean the compound";
}
if else statement
In python, a colon (:) is written after the else keyword.
if condition:
if code
else :
else code
Let's look at an example:
age = 34
if age < 18:
print('kid you are!')
else:
print('You are not a kid')
In JavaScript, the code that belongs to the elseclause is enclosed in curly braces.
if (condition)
{
if code
}
else
{
else code
}
Let's look at an example.
var time = new Date().getHours();
if (time < 5) {
task = "Cleaning the house";
} else {
task = "Feeding the sheep";
}
Multiple conditions
In python elif keyword is used when dealing with multiple conditions. After every condition we write a semicolon(:) and the code that belongs to the condition is indented in the next line.
if condition1:
code
elif condition2:
code
elif condition3:
code
else:
code
Let's look at an example.
age = 40
if age < 18:
print('kid you are!')
elif age >=18 and age < 29:
print('You are a youngst!')
elif age >=29 and age < 35:
print('You are middle aged!')
else:
print('You are above middle age !')
In JavaScript else if keyword is used when dealing with multiple conditions. The conditions are surrounded by parenthesis.
if(condition1)
{
//code
}
else if(condition2)
{
// code
}
else if (condition3)
{
//code
}
else
{
//code
}
Let's look at an example.
var time = new Date().getHours();
if (time < 5) {
task = "Cleaning the house";
} else if (time < 15) {
task = "Feeding the sheep";
} else {
task = "Watching a movie";
}
Switch
Python does not have this type of built-in control structure i.e switch
In JavaScript, a switch is utilized to pick what happens depends on the worth of an articulation.
switch(expression)
{
case 1:
code
break;
case 2:
code
break;
case 3:
code
break;
default:
code
}
Let's look at an example.
switch (new Month().getMonth()) {
case 1:
month = "January";
break;
case 2:
day = "April";
break;
case 3:
day = "May";
break;
case 4:
day = "June";
break;
case 5:
day = "August";
break;
case 6:
day = "September";
break;
default:
No such a month;
}
Loops
A loop is a control structure that repeats a series of instructions until a specified condition is reached.
For Loops
We write a for loop in Python as shown below.
for x in range(k):
code
The syntax of writing a for loop in JavaScript is as follows.
for(var x = 1; x >n ; x++)
{
code
}
While Loop
It is a pre-test loop in which the condition is evaluated before the loop executes.
In Python, we compose the watchword while trailed by the condition, and afterward, a colon (:), and afterward the body of the circle in another line in an indented mode.
while condition: code
In JavaScript, the linguistic structure is the same. The distinctions are that we need to encompass the condition with enclosures and the body of the circle with wavy supports.
while(condition)
{
code
}
Do while loops
This is a post-test loop. The condition is evaluated after the loop executes
In python, we do not have such a control structure (do-while loop)
In JavaScript, this loop will always be executed
once.
do { code } while (condition);
Functions
A Function is a block of code that plays out a specific undertaking. In Python, we utilize a watchword def which is trailed by the name of the function. Then the parameters are enclosed in parenthesis and then a full colon immediately after the parameters are enclosed in parenthesis, after which an indented code is written.
def function_name ( x1 , x2 , x3 , ...):
code
In JavaScript, the lone contrasts are that we characterize a capacity utilizing the 'work catchphrase 'and encompass the body of the capacity with 'wavy supports'.
function function_name (x1,x2,x3,...)
{
code
}
Object Oriented Programming(OOP)
OOP is the development of programs in terms of objects that interacts with each other.
Both Python and JavaScript do uphold Object-Oriented programming.
The syntax of defining a class in both Python and JavaScript is almost the same there is a slight difference.
In python a colon is written after the keyword class whereas curlybraces are used in Javascript.
Python
class Rectangle:
code
JavaScript
class Rectangle
{
code
}
Constructor and Attributes
A constructor is a member function of a class that executes automatically whenever an object is created.
In Python, a constructor that instates the new example is known as init with two driving and following highlights. This strategy is called consequently when an example of the class is made to introduce it's ascribes. Its boundaries list characterizes the qualities that we need to pass to make the example. This rundown begins with self as the principal boundary.
class Rectangle:
def _init _(self , length , width):
self.length= length
self.width=width
In JavaScript, a constructor strategy is called constructor and it has a rundown of boundaries as python has.
class Rectangle
{
constructor(length,width){
this.length=length;
this.width=width;
}
}
NB
In python, we use self to refer to an instance of a class.
i.e
self.attribute= esteem
While in JavaScript we use this to allude to an occurrence of a class.
i.e
this.attribute=value;
Methods In Python and JavaScript.
We characterize strategies with the 'def watchword' trailed by the name of the technique and the boundaries list inside brackets. This boundaries list begins with the self boundary to allude to the occurrence that is calling the technique. After this rundown, we compose a colon (:) and afterward on another line the body of the technique indented.
class Rectangle:
def _init _(self , length , width):
self.length= length
self.width=width
def calc_area(self):
return self.length*width
In javascript the syntax is as shown below.
class Rectangle
{
constructor(length,width)
{
this.length=length;
this.width=width;
}
calcArea()
{
return this.length*width;
}
}
Learning curve
Although these two languages, Python and Javascript, are have got the differences listed above but
In terms of learning and implementing, Python is much easier than JavaScript because its syntax is much easier to understand and also implementing.
Top comments (1)
Perfect Insight