Javascript and Python

Overview

Both Javascript and Python can be used on the davinci platform. Javascript, being the native language of the browser, is the primary language used.

This page describes the similarities and differences between Javascript and Python for Python users who want to understand Javascript.

Variable Declaration

Variable declaration in Python is simple, you simply assign a variable to a value.

a = 10;

The equivalent in Javascript is the following.

let a = 10;

However, Javascript also accomodates the following ways to declare a variable.

a = 10; let a = 10; var b = 10; const c = 10;

Objects/Maps

In Python, you create a simple object/map as follows, which is the same as Javsacript. (where if you were declaring the variable, you would probably precede it with "let")

obj = {};

Arrays

Creating an array in Python and Javascript is again the same.

list = []

Functions

Functions in Python and Javscript are similar, however, the syntax for declaring a function is different.
def add(a, b): return a+b

In Javascript this becomes
function add(a, b){ return a+b; }

Anonymous Functions

Anonymous functions are functions that do not (necessarily) have a name. They are usually constructed on the fly and used once.

In Python, an anonymous function is declared using the lambda syntax.

lambda x, y: x + y

Javascript has multliple ways to represent the same anonymous function.
function(a,b){ return a+b; } (a,b)=>a+b; (a,b)=>{ return a+b; }