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.
- Introduction
- variables
- Some different variables type
- Scopes
- Keywords
- Operators
- Type casting
- Classes
- Some other minor stuff
- Conclusion
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
- Numbers: The only difference is that there is only float and integers in js, without complex numbers
- Strings: There is no multiline strings (except using \ in a template literal, more on that later)
- Boolean:
True
istrue
,False
isfalse
(lowercase) - Lists: It is called
arrays
and it is also declared with[]
- Dictionarys, tuples: They are
objects
, declared by{}
- Sets: You are not using this
- None: It is
undefined
, there is alsonull
andNaN
which they are not the same
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
}
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')
}
}
+
-
*
/
+=
-=
*=
/=
=
are the same
We have ++
meaning += 1
and --
meaning -= 1
not
is converted into !
and
is converted into &&
or
is converted into ||
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) |
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);
print('a')
-> console.log('a')
anObject['a']
-> anObject.a
or anObject['a']
lambda x,y:x+y
-> (x,y)=>{return x+y}
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