Skip to content

Instantly share code, notes, and snippets.

@cleverdumb
Last active July 23, 2022 08:05
Show Gist options
  • Save cleverdumb/2d4d6ecab9cd00fab8359d1383d4af32 to your computer and use it in GitHub Desktop.
Save cleverdumb/2d4d6ecab9cd00fab8359d1383d4af32 to your computer and use it in GitHub Desktop.
Ultimate guide of python to js

How to switch python to javascript?

Introduction

Python and js are both quite popular. Some python users want to start doing web-dev, which most people used js to do so.
Although many people want to switch to js, some people struggle to do so.

Table of contents

  1. Introduction
  2. variables
  3. Some different variables type
  4. Scopes
  5. Keywords
  6. Operators
  7. Type casting
  8. Classes
  9. Some other minor stuff
  10. Conclusion

Variables

The concept of variables are basically the same.
The one thing that is different is that for good practise, always define a variable with let and constant variables with const

Examples:

a = 1
b = 'abc'
c = 'a constant'
let a = 1
let b = 'abc'
const c = 'a constant'

The keywords go before the variable name

Some different variables type

  1. Numbers: The only difference is that there is only float and integers in js, without complex numbers
  2. Strings: There is no multiline strings (except using \ in a template literal, more on that later)
  3. Boolean: True is true, False is false (lowercase)
  4. Lists: It is called arrays and it is also declared with []
  5. Dictionarys, tuples: They are objects, declared by {}
  6. Sets: You are not using this
  7. None: It is undefined, there is also null and NaN which they are not the same

Scopes

Tabs and indents doesnt matter in js because it will all get squashed up in the compiler.
The colons are turned into {} to define a scope

For example:

def myFunction():
  # something

is turned into

function myFunction() {
  // something
}

Keywords

def is function
elif is else if
Statements in a if must be put inside brackets

For example:

a = 1
def myFunction():
  if a==0:
    print('a is 0')
  elif a==1:
    print('a is 1')
  else:
    print('a is neither 0 or 1')

Is converted into

let a = 1;
function myFunction() {
  if (a==0) {
    console.log('a is 0')
  }
  else if (a==1) {
    console.log('a is 1')
  }
  else {
    console.log('a is neither 0 or 1')
  }
}

Operators

+ - * / += -= *= /= = are the same
We have ++ meaning += 1 and -- meaning -= 1
not is converted into !
and is converted into &&
or is converted into ||

Type casting

This is probably the part that most pythoners struggle with
Here is a table of how to convert types:

First type Second type Function
Number String number.toString
String Integer parseInt(string)
String Float parseFloat(string)
Boolean Number +(Boolean)

Classes

They are still declared with class
__init__ is constructor
self is this
Methods doesn't require this in the parameters
To create a new instance, use new className(parameters)

Example:

class Person:
  def __init__(self,age):
    self.age = age
  def output(self):
    print(self.age)

tom = Person(10)

Is converted to:

class Person {
  constructor(age) {
    this.age = age;
  }
  output() {
    console.log(this.age)
  }
}

let tom = new Person(10);

Some other minor stuff

print('a') -> console.log('a')
anObject['a'] -> anObject.a or anObject['a']
lambda x,y:x+y -> (x,y)=>{return x+y}

Conclusion

There are quite a lot of differences. This guide covered some of the basic ones, hope this helps!
If I missed something very important or made a mistake, comment down there.

By cleverdumb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment