In this blog, I will share my experience of working on both languages at the same time. Let’s get started.
Below are the code snippets which describe the major syntax differences. Can you observe how different they are?
The syntax b/w Javascript and Python are very different as shown in the above sample blocks. Sometimes I make mistakes by using one’s syntax in another. To avoid this these IDE’s are really helpful– IntelliJ (Python) and Vscode (Javascript).
Below are the major differences that I came across:-
- Python code uses tabs for a code block whereas JS uses { }
- Python uses ‘#’ for comment while JS use ‘//’
- Python uses the ‘print’ keyword whereas JS uses ‘console’ keyword to debug anything in the console panel.
- Python Function uses a ‘def’ keyword to define function whereas JS uses ‘function’ keyword
- The constructor of the Python class is defined by ‘__init__’ whereas JS uses a normal constructor.
- The semicolon is not mandatory in both languages to define the end of a statement. But we use it in JS because if we don’t apply it, JS engine will apply it automatically and create unnecessary bugs in the code.
Approaches to implement task in different languages
Every language has its own beauty. While solving any task with NodeJS I need to think in a different way than implementing them in Python. In some scenarios, Python wins and in some NodeJS.
Just a small example-
To create a Task manager backend in NodeJS, I need to use Express. To replicate the same functionality in Python, I need to use Flask.
Checkout repo for basic task manager https://github.com/agarwalparas/task-manager
However, later on, if my backend needs a functionality of Machine learning to manage tasks and prioritize them on the basis of users’ behaviour, then I will surely use Python.
Whereas if my backend needs high speed to list tasks or to search from tasks or for faster real time updates of tasks within the team, then I will surely use NodeJS.
So, it is really tough to decide which language to use in which project. But it is fairly straightforward to say which language can be used for a particular task.
After some experience, I have figured out a way to decide which language is better for a project.
NodeJS for Chat Applications and Realtime Apps whereas Python for Analytics, Machine Learning, Command Line Utilities.
Some important concepts
F String in Python and Template literals in Javascript
The F string and template literals are great new ways to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, they are also faster!
Decorators in Python and Callback Function in Javascript
Decorators and Callback Function are very powerful and useful tools since they allow programmers to modify the behavior of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function whereas in Javascript the function passed as argument is called callback function.
Async/Await in NodeJS
Before async/await, JS used promises but its code was a little complex to debug and caused callback problems.
Then JS introduced a neat syntax to work with promises in a more comfortable fashion. It’s called “async/await” and is relatively easy to understand and use.
Conclusion
So all in all it’s a very exciting journey. Both languages have some pros and cons. But isn’t it the same with everything. Different languages exist because there is no one-size fits all approach to programming. In fact, their existence gives us tools to help create more robust products. My experience of working with Python and JS simultaneously has helped me gain more exposure to the world of programming languages and I now look forward to learning more about other unknown languages.