Learning JavaScript with a Python background

Learning JavaScript with a Python background

Before learning JavaScript

As someone who consumed a large number of python tutorials (bad idea by the way) when I was curious about programming and making chatbots, one thing they had in common was instructors saying* "Once you learn one language, it's easy to learn others".

Well, never has there been a statement that is itself so true and yet so false.

At the end of 2021, I decided to learn Javascript for an idea I had in mind. Initially, I thought of using the Python Django framework instead of learning a whole new language but I only ever used Python for Reddit/Discord bots and to be honest, I bought into the whole "Js web development train."

Learning Javascript

It's been over a month since I started learning JavaScript and I'm yet to find the exact words to describe the process so far.

While it was certainly easy to immediately understand fundamentals like data types and structures as this cuts across programming languages, the syntax and several other differences were challenging in the first few weeks.

Differences I've encountered

  • The semicolon and brackets

    Two things that will immediately frustrate any Python developer who uses Javascript are the many semicolons and brackets they'll have to write. Just for context:
console.log('Zaun') ; 

print('Zaun')

While your code will run without the semicolon at the end, it is best practice to always include the semi-colon. To save time and also make sure you never forget, you can download the Prettier extension. Also, your if conditions and loops are followed by brackets which are then succeeded by curly brackets where the statements reside.

  • Variable declarations

    const, let, var, the three keywords that welcomed me to Javascript. These keywords used for declaring variables in Js are non-existent in python. Js mandates you use the const, let or var keyword before the variable name depending on the use case.
let champion ='Jinx'; //Js code

champion = 'Jinx'  #Python code
  • List vs. Arrays

    This took a while for me to wrap my head around because Javascript has only Arrays and no lists. Essentially both are used to store data or collections of data and they have a definite order. Just like with lists in Python we can store different data types in JavaScript arrays. Python also has Arrays but this data structure is used to store single data type values and are created by importing the array module.
#Python array syntax
from array import *
variableName = array (data type, value list)
  • Dictionary vs. Objects

    Another key term missing in Js is Dictionary, which is what many python developers' go to when storing data values in key-value pairs. Javascript makes up for this absence with the Object type. While the Object type is an unordered collection of key-value pairs, it can also be used as a dictionary object. With python, however, the key has to be a hashable object like numbers or strings.
#Python Dictionary:
py_champions = { 'name' : 'Jinx' , 'year' : 2013)

//Javascript Object:
JsChampions = {name :'Vi',  year: 2012);
  • The camelCase - snake_case and CapsCase mixups

    The two languages also have different style conventions with regards to variable, function and object names. While python uses snake_case in naming variables, functions, methods, attributes and CapsCase for classes, Js adopts the camelCase convention for both variables and functions. Constants and Global variables are written in uppercase.

All in all, I can say that learning Js showed that it's really had to shake off habits whether old or new. It had been months since I sat down to write a line of python but I still found myself using def to start a function and print to log code to the console.

Having to learn another syntax when you're already used to things being done one way can be confusing at first. You may mix things up but it does get better with practice and also code formatters help make the code less "Pythonesque".