Python vs JavaScript
Tags: python, javascript, beginners
Published: 2023-11-07 18:12:04

Python and JavaScript are both widely used programming languages, each with its unique features and capabilities. Python is a general-purpose programming language that is widely used in scientific and specialized applications. In addition to this, it is also used for web development. Python is used for back-end development, which is the part of web development that deals with creating the elements that a user doesn’t see. JavaScript, on the other hand, can be used to develop both the front-end and the back-end of an application. The front-end is the part of an application that the user both sees and interacts with. While Python and JavaScript are both incredibly powerful programming languages, they have notable differences in their syntax which shape the way their code is structured and written. 

## Code Blocks

In Python, indentation is used to define code blocks. Indentation is created by using spaces or tabs. When multiple lines of code are written at the same level of indentation, they are considered to be part of the same code block. 

```python
while x < 10:
    print(x)
    x = x + 1
```
In JavaScript, curly braces are used to define code blocks. 

```javascript
while (x < 10) {
    console.log(x)
    x = x + 1
}
```

## Variable Definitions

To assign a variable in Python, we write a variable name followed by an equal (`=`) sign and the value that will be assigned to the variable.

<u>Example:</u>

`my_variable = "Hi there"`

Declaring variables in JavaScript is similar to Python. The main difference, however, is that in JavaScript, the keywords `const`, `let`, or `var` are used to declare the variable (though `const` and `let` are usually preferred to `var` nowadays). When using `const` to declare a variable, the variable cannot be redeclared or reassigned. The `let` keyword also does not allow a variable to be redeclared, but it does allow the variable to be reassigned. This is useful when you know the value of a variable will change. 

<u>Example:</u>

`const name = "Steven"`
`let counter = 0`


## Variable Naming Conventions

When creating variable names in Python, it is common practice to use snake case. Snake case is a style in which spaces between words are replaced by an underscore (`_`).

<u>Example:</u>

`my_variable`

In JavaScript, it is common practice to use the camel case naming convention. Camel case is the practice of writing phrases without spaces or punctuation and with capitalized words. 

<u>Example:</u>

`myVariable`

While it is not required to use these naming conventions in order for your code to execute properly, these conventions are considered to be good practice and are widely used for their respective languages.


## Primitive Data Types

A primitive data type is considered to be either a data type that is built into a programming language, or one that could be identified as a basic structure for building more sophisticated data types. In Python, there are four primitive data types. These data types are:
  1. Integer
  2. Float
  3. Boolean
  4. String

Conversely, JavaScript has seven different primitive data types:
  1. Number
  2. Boolean
  3. String
  4. BigInt
  5. Symbol
  6. Undefined 
  7. Null


## The `undefined` Value

Python doesn't allow you to declare a variable without assigning it an initial value. JavaScript, on the other hand, does allow this. When a variable is declared without assigning an initial value, the variable is automatically assigned a special value called `undefined`.


## None vs Null

In Python, when a variable doesn't have an assigned value at a particular point in the program, it is given a special value called `None`. None is not the same as 0, False, or an empty string. It is its own datatype (NoneType).

<u>Example:</u>

`x= None`

JavaScript's version of `None` is `null`. `null` represents the intentional absence of any object value.

<u>Example:</u>

`let x = null`


## Logical Operators

Python has three logical operators:
  1. and
  2. or
  3. not

JavaScript also has three logical operators that mean the same thing but are represented differently.
  1. && (and)
  2. || (or)
  3. ! (not)


## Value Type Comparison

Python use the `==` operator to compare if two values and their data types are equal.

```python
1 == 1   #True
1 == "1" #False
```
Python also has a 'not equal' operator `(!=)` that returns True if the values are not equal and False if they are equal. 

```python
1 != 1    #False
1 != "1"  #True
```

JavaScript has four equality operators. These are the strict equality operator (`===`), the strict inequality operator (`!==`), the loose equality operator (`==`), and the loose inequality operator (`!=`). The strict equality operator returns true if two values are equal without performing type conversions, and the strict inequality operator returns true if two values are not equal without performing type conversions. 

```javascript
10 === 10           //true
true === 1          //false
null === undefined  //false

10 !== 10           //false
10 !== "10"         //true 
[] !== ""           //true
```
While the loose equality operator also returns true if two values are equal, it will additionally return true if it can perform a type conversion (e.g., changing the string '10' into the number 10) that makes the two values equal. The loose inequality operator does the opposite and returns true if two values are not equal, performing type conversions as necessary. 

```javascript
10 == 10            //true
true == 1           //true
null == undefined   //true
"0" == false        //true

10 != 10            //false
10 != "10"          //false
[] != ""            //false
```

## Outputs

To print a value to the console in Python, we use the `print()` function. The value that we want to print is passed into that function

<u>Example:</u>

`print("Hello World")`

JavaScript uses the `console.log()` function to print to the console.

<u>Example:</u>

`console.log("Hello World")`

While Python and JavaScript function in similar ways, there are many small syntactical differences that determine if code is executed properly. While this blog doesn't discuss every difference between the two languages, the topics covered here are some of the most basic discrepancies that are important to understand when switching between languages. Understanding these syntax differences is crucial for developers transitioning between the two languages and allows for informed, effective coding in both Python and JavaScript.