DEV Community

Cover image for A Simple Comparison of JavaScript and Python
Mac Little
Mac Little

Posted on • Updated on

A Simple Comparison of JavaScript and Python

The Goal of this Post

In my many previous attempts to learn to code, I probably spent way too much time thinking about which language I should learn first. For the most part, I was torn between Python or "Java-something" (like I said, many previous attempts).

At the time, it seemed like every blog I read or person I talked to said the same thing: "It doesn't matter, just pick one". And while I whole heartedly agree with them today, analysis paralysis definitely got the better of me and my programming goals and I'm sure it stands in the way of others just trying to get their sea legs.

So here's my hope for this blog post:

  1. Again, fully endorse "It doesn't matter, just pick one" message.
  2. But at the same time, give people like my younger self some simple explanations that will get them closer to following step #1.

JavaScript is Scripted

Probably the biggest difference between JavaScript and Python is how they interact with your code or "flow".

JavaScript is considered a "scripting" language, which basically means it tells our code what to do. While probably a little too on the nose, think about actors. For the most part, actors just read the script that's given to them and boom, you have a movie. Sir Ian McKellen gives a great explanation in this clip.

In JavaScript, we provide the script or directions for our programs to follow. In this code example, we've created a function named action that gets our actor to say their line when prompted by our program. How will they know the line? Well, we give it to them. How will they know when to say it? We tell them to do it when we say action and reference them.

Alt Text

let sirIan = {
  name: 'Sir Ian McKellen',
  role: 'Gandalf',
  line: 'WIZARD, YOU SHALL NOT PASS!'

};

const action = ({line}) => {

  console.log(line);

};

action(sirIan); // will print 'WIZARD, YOU SHALL NOT PASS!' to the console

But what if we told our actor to say their lines before we gave them the script? Well, that's bad news.

const action = ({line}) => {

  console.log(line);

};

action(arnold); // will throw an error because arnold isn't defined yet

let arnold = {
  name: 'Arnold Schwarzenegger',
  role: ' Major Alan "Dutch" Schaefer',
  line: 'GET TO THE CHOPPA!'
};

Even though JavaScript functions and variables can be distributed across a file or program, it still goes one direction: downhill. So even though we eventually define Arnold, it's too late. No one can get to the choppa.

Python is Object-Oriented

Python, on the other hand, is an "object-oriented" language. As I wrote about a few weeks ago, object oriented simply means trying our best to mimic real-world objects (whether they're cars, houses, fruits, etc.) in the digital world. By building programs with object-oriented languages like Python, we create objects that have unique attributes (like color, shape or name) and methods, and then let them "run free" in our digital environment.

In the Python code below, I'm essentially doing the same thing I did in the JavaScript code block, with slightly different syntax. Instead of creating a standalone actor named 'ian', I first had to create an Actor "class" which is essentially the building blocks for any future actors I create.

Then, I can create an "ian" actor with several unique properties, but it also has access to the action function we created in the Actor class, even though I don't explicitly give it to him. In theory, this allows Sir Ian to say his line whenever he wants. He has his own power!

class Actor:    
  def action(self):
    print(self.line)

ian = Actor()
ian.name = "Sir Ian McKellen"
ian.role = "Gandalf"
ian.line = "WIZARD, YOU SHALL NOT PASS!"

# prints 'WIZARD, YOU SHALL NOT PASS!'
ian.action();

Funnily enough, Python can also serve as a scripting language as well, but we'll avoid diving deep into that today. Just know that Python can tell actors like Sir Ian to do something too, but JavaScript is considered the industry standard for scripting languages.

How they're used in the real world

In so many words, JavaScript is the master of the web. It practically tells everything you interact with online to "do something". The browser you're using right now has a JavaScript engine that tells your computer to render these words on the screen. When you leave a comment or a "like" when you're done reading this, you're telling this page to make a record of your interaction.

In fact, if you're using Firefox or Chrome, you can used Command + Option + I to open up a window where you can play with live JavaScript. It's quite literally everywhere.

The web is a giant latticework of users and programs just telling each other what to do: give me this video, post this message, save this playlist.

But what is that video, message, or playlist made of? Well, foundationally, it's just an object. So, it's very possible that you could be using JavaScript to do something and that "thing" could be an object made with Python.

Because languages like Python can be used for modeling objects so well, it should come as no surprise that the applications for Python are almost limitless, which makes a great language to learn if you're just starting out or if you're a team of data scientists trying to model the human brain for an artificial intelligence project (that will be next week's blog post).

Conclusion

The biggest obstacle in learning how to do something is sometimes the learning itself. Meaning, you can only learn so much before it becomes time to do something with what you've learned.

It's my hope that after learning a little about the differences (and similarities) of JavaScript and Python, you'll feel more empowered to start typing in some code yourself. Again, which language you choose doesn't really matter, so choose one and let it rip.

Top comments (0)