JavaScript to Python- a few differences in syntax

megharrison92

megharrison92

Posted on July 1, 2023

JavaScript to Python- a few differences in syntax

When you are learning a new programming language it can be hard to keep the syntax straight in-between the two languages. Here is a quick reference of some basic differences between JavaScript and Python.

Naming Conventions-

  • JavaScript uses lowerCamelCase as a naming convention.

  • Python uses the snake_case as a naming convention.

// JavaScript
aNewCupOfCoffee

# Python
a_new_cup_of_coffee
Enter fullscreen mode Exit fullscreen mode

Declaring variables-

  • JavaScript uses const, let, and var to declare variables.
  • To declare a variable in Python, it is set to the value- variable = value.
// Variable in JavaScript
const kaiBear = "super fluffy pupper"

# Variable in Python
kai_bear = "super fluffy pupper"
Enter fullscreen mode Exit fullscreen mode

Creating Functions-

  • In JavaScript we create functions by using the function keyword and surrounding the function body in curly braces.

  • To create a function in Python use the def keyword and add a colon after the parentheses.

// JavaScript function
function newRamenMenu(){
   // write code here
}
# Python function
def new_ramen_menu():
    # write code here
Enter fullscreen mode Exit fullscreen mode

Code Blocks-

  • JavaScript uses curly braces to group statements together.
  • To create a code block in Python you indent (press the space bar 4 times) continuous lines of code.
// A code block in JavaScript
if (a > 3){
  console.log(a);
}

# A code block in Python
if a > 3:
    print(a)
Enter fullscreen mode Exit fullscreen mode

Creating Comments-
Single line comments

  • Single line comments are written in JavaScript with two slashes(//).

  • Python writes single line comments with a hashtag, or the pound sign (#)

// This is a comment in JavaScript

# This is a comment in Python
Enter fullscreen mode Exit fullscreen mode

Multi-line comments

  • Multi-line comments are written in JavaScript by starting with a /* and ending with */.

  • Python writes multi-line comments with a hashtag, or the pound sign (#) at the beginning of each line.

/*
This is a multi-line 
comment in JavaScript.
*/

#This is a multi-line
#comment in JavaScript.
Enter fullscreen mode Exit fullscreen mode

Resources-
https://www.freecodecamp.org/news/python-vs-javascript-what-are-the-key-differences-between-the-two-popular-programming-languages/
https://www.educba.com/python-vs-javascript/

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
megharrison92
megharrison92

Posted on July 1, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About