Javascript for Python Programmers

Overview


At a broad level, Python and Javascript are similar languages. They have the same language constructs, with different syntax.

Variable Declaration

Variables declarations in Python are simple. You simply assign a value to a variable.


a=10
				


In Javascript, this can be more complex. The typical way to accomplish the same thing is Javscript is as follows.


let a=10;
				


There is a keyword, let, which indicates that the statement is a variable declaration. There are other keywords that can be used, for example, var or const.

Functions

Both languages have functions, with slightly different syntax.


def add(a, b):
    return a+b
				


The standard way to declare the same function in Javascript is given.


function add(a, b){
    return a+b;
}
    
				


Arrays

Both languages have arrays, and the syntax is fairly similar.

The following code creates a new array with three numbers in it, and then adds another number at the end.


list = [1,2,3]
list.append(4)
                


In Javascript, the code looks like


let list = [1,2,3];
list.push(4);    
				


Objects

Both languages have objects, that function as hashmaps. They are declared in a similar way, with new properties being able to be assigned in both.


obj = {}
obj['one'] = 1
obj['two'] = 2
				


Javascript simplifies property syntax by supporting using the period to reference a property. (Limitations on the property name exists when using the period syntax)


let obj = {};
obj['one'] = 1;
obj.two = 2;