Base syntax differences between Python and JavaScript – part 1

Here are some of the syntax differences between Python and JavaScript:

Syntax for defining variables:
In Python, you can simply assign a value to a variable without specifying its type: my_var = 10.
In JavaScript, you need to use the var, let, or const keywords to declare a variable: let myVar = 10;.
Syntax for defining functions:
In Python, you use the def keyword to define a function: def my_func():.
In JavaScript, you use the function keyword to define a function: function myFunc() { }.
Syntax for conditional statements:
In Python, you use if, elif, and else keywords for conditional statements:

if x > 5:
    print("x is greater than 5")
elif x == 5:
    print("x is equal to 5")
else:
    print("x is less than 5")

In JavaScript, you use if, else if, and else for conditional statements:

if (x > 5) {
console.log("x is greater than 5");
} else if (x == 5) {
console.log("x is equal to 5");
} else {
console.log("x is less than 5");
}

Syntax for loops:
In Python, you use for and while loops:

for i in range(5):
    print(i)

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

In JavaScript, you use for, while, and do-while loops:

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

while (x < 10) {
console.log(x);
x++;
}

do {
console.log(x);
x++;
} while (x < 10);

Syntax for comments:
In Python, you use the # character to comment a single line or triple quotes ”’ to comment multiple lines:

# This is a single line comment

'''
This is a
multi-line
comment
'''

In JavaScript, you use // to comment a single line and /* */ to comment multiple lines:

// This is a single line comment

/*
This is a
multi-line
comment
*/

These are some of the main syntax differences between Python and JavaScript, but there are many more. It’s important to be aware of these differences when switching between the two languages.

See also  How to add a click on a row in a table and keep the links active
Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *