A new year brings new opportunities for developers and those trying to learn to code. One of the most common dilemmas these people face is figuring out how and where to go when learning a new programming language from scratch. There are countless options and many different paths you can take.
Arguably two of the most popular and talked about programming languages in the world are JavaScript and Python. If you search for JavaScript and Python jobs on any job platform, you’re likely to see hundreds, if not thousands, of results. The market is and will always be hot for these two languages and that is not going to change any time soon. Both languages also have some similarities. Both are C-based languages, which basically means they have general purpose, functions, and other logics.
Why should you learn JavaScript and Python in 2022?
Dynamic and Static
One of the highlights is that JavaScript and Python are dynamic languages. In a sense, a dynamic programming language does not require variables to be explicitly declared before using them. And vice versa, a static programming language will have to declare the data type when creating the variable. All of this will become clearer in the examples below.
Java example:
String myName; // Biến không có kiểu myName = "Tony Stark"; // Biến có kiểu chuỗi myName = 24; // Biến đã tự động thay đổi kiểu của nó thành số
If you run this Java code, you will get a compile error or a runtime error. Other data types cannot be assigned to variables. You can just use another string like “Steve Rogers”.
JavaScript example:
let myName; // Biến không có kiểu myName = "Tony Stark"; // Biến có kiểu chuỗi myName = 24; // Biến đã tự động thay đổi kiểu của nó thành số
Now if you run this JavaScript code, you won’t get an error because it’s perfectly valid. The value of the variable is now the number 24.
Python example:
my_name = "Tony Stark" # Biến có kiểu chuỗi my_name = 24 # Biến đã tự động thay đổi kiểu của nó thành int
Similarly if you run this Python code, you won’t get an error because it’s perfectly valid. The value of the current variable is Int 24.
Data types
Data types are basically ways to store data inside an application. The data types specify what can be stored and how it can be managed. Below you will see a list of some of the data types that each language has.
JavaScript data types:
- String type: string
- Number type: number
- Boolean type: boolean
- Mapping type: object
- Sequence type: array
Python data types:
- String type: string
- Numeric types: int, float, complex
- Boolean type: boolean
- Mapping type: dict
- Sequence type: list, tuple, range
Syntax comparison
Next, we’ll compare the syntax of both programming languages so you can see how easy it is to switch between the two. First, we will learn some of the differences between the two languages.
JavaScript
- Use semicolon
- Use curly braces for code blocks
- Use the CamelCase naming convention for variables, e.g. firstName
- Use console.log to output console messages
Functions that use this syntax:
function myFunc() { console.log('Hello World'); } const myFunc2 = () => { console.log('Hello World 2'); }; myFunc(); myFunc2();
Python
- Do not use semicolons
- Do not use curly braces
- Use the Snake Case naming convention for instance variables first_name
- Use indentation for code blocks
- Use print to output the message to the console
Functions that use this syntax:
def my_func(): print('Hello World') my_func()
String type
Both languages output a variable of type string
JavaScript syntax:
let myName = "Tony Stark"; console.log(typeof myName); // string
Python syntax:
my_name = "Tony Stark" print(type(my_name)) # str
Number type
In this example, both variables output a number.
JavaScript syntax:
let num = 9000; let num2 = 9.0; console.log(typeof num); // Number console.log(typeof num2); // Number
Python syntax:
In this example, Python has a difference between a number and a real number because they are built in different data types.
num = 9000 num_2 = 9.0 print(type(num)) # Int print(type(num_2)) # Float
Boolean style
The output is almost identical, the only difference is that JavaScript uses lowercase “t” for true while Python uses uppercase “T”.
JavaScript Syntax
let wizard = true; console.log(wizard); // boolean
Python syntax
wizard = True print(wizard) # bool
Mapping style
JavaScript uses object data structure while Python uses dictionary data structure. From looking at the examples, you can see that they are very similar. The difference is that JavaScript doesn’t require quotes for keys while Python requires them for keys in key value pairs.
JavaScript Syntax
const myprofile = { name: 'Tony Stark', age: 48, superhero: 'Iron Man', }; console.log(myprofile);
Python syntax
my_profile = { "name": "Tony Stark", "age": 48, "superhero": "Iron Man", }; print(my_profile);
Sequence style
JavaScript uses Array which is an object data structure type while Python uses a list data structure – a list. As you can see they have many syntactical similarities.
JavaScript Syntax
const myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; console.log(typeof myArr); // object
Python syntax
myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(type(myArr)) # list
Also, if you want to learn python then you can read this paragraph.