13 Jun

Variables are the names you give to computer memory locations which are used to store values in a computer program. So, they are the basic units for any programming language. Here, I'm not aiming to deep down into the variables but highlight the major differences in variable usage and behavior between JS and Python.


Declaration

JS

  • Declared using the var, let, or const keywords

Python

  • variables are created simply by assigning a value. No explicit declar­ation is required.



Naming Convention

JS

  • Variables in JavaScript typically follow camelCase naming convention
> myJSVarInCamelCase

Python

  • Variables in Python typically follow snake_case naming convention
>>> my_python_var_in_snake_case


Initialization

JS

  • Variables can be declared without an initial value, and they will have the value undefined until assigned a value.
  • variables do not have default values.

Python

  • Variables need to be assigned an initial value when they are created. Otherwise, a NameError will occur.
>>> print(not_initiated_variable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>NameError: name 'not_initiated_variable' is not defined>>> initiate_variable = None
>>> print(initiate_variable)
None


Variable Assignment

JS

  • variables can be assigned values using the equal sign (=)

Python

  • variables can be assigned values using the equals sign (=) or the colon (:)


Primitive data types

JS

  • undefined
  • Boolean
  • String
  • Number
  • BigInt
  • Symbol
  • null

Python

  • int
  • float
  • bool
  • str


No Value

JS

  • The null and undefined values are different. 
  • null indicates that a variable has no value, 
  • while undefined indicates that a variable has not yet been defined.

Python

  • There is only one value for "no value", which is None

 

Dynami­cally typed

Both Js and Python are dynamically types languages. 

In a dynamically types language, variables can receive different data types over time. We don’t need to specify variable type, whether the value in the variable is an integer, string, or any other data type like float, double, etc. The programming language engine automatically understands the data type at runtime. 

Moreover, We can re-assign any variable with any other value having another data type.

JS

  • Type type of a variable can figured out using typeof operator
typeof "John"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'John', age:34}  // Returns "object"
typeof new Date()             // Returns "object"
typeof function () {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object"


Python

  • The type of a variable can be checked using the built-in function type().
type("John")                      # Returns <class 'str'>
type(3.14)                        # Returns <class 'float'>                  
type(False)                       # Returns <class 'bool'>
type([1,2,3,4])                   # Returns <class 'list'>
type({"name":'John', "age":34})   # Returns <class 'dict'>
type(datetime)                    # Returns <class 'module'>
type(datetime.datetime)           # Returns <class 'type'>

def func1:
     print("fun11")
type(func1)                       # Returns <class 'function'>
type(myCar)                       # Returns NameError: name 'myCar' is not defined
type(None)                        # Returns <class 'NoneType'>



Equality

JS

  • two variables are equal if they have the same value, regardless of their type

Python

  • two variables are equal if they have the same type and the same value.


 Mutability

JS

  • variables are mutable by default. 
  • This means that their values can be changed after they are assigned.

Python

  • variables are immutable by default. 
  • This means that their values cannot be changed after they are assigned.


Type coercion

Type coercion refers to the automatic conversion of one data type to another, usually performed by the language interpreter. 

JS

  • Type coercion is automatic. 
  • This means that if you try to perform an operation on two values of different types, the values will be automa­tically converted to the same type before the operation is performed.

Python

  • Type coercion is not automatic and not supported in the python. 
  • Although Type casting is supported, where you must explicitly convert values to the desired type before performing an operation on them.

I've written a nice article with examples and different use cases related to type coercion in both js and python. Please check it here.


Scoping

JS

  • Variables have functi­on-­level scope, meaning they are accessible within the function they are defined in. 
  • Variables declared with var are also hoisted, allowing them to be accessed before their actual declar­ation.
  • Variables declared outside of any function or block have global scope and can be accessed from anywhere in the code.

Python

  • Variables have block-­level scope, meaning they are accessible within the block they are defined in. 
  • Python does not have variable hoisting.
  • Variables declared outside of any function or block have global scope, but accessing them from within a function requires the global keyword.



Comments
* The email will not be published on the website.