Join me and let’s dissects the data models of Python and JavaScript, those fundamental building blocks that shape how data is organized and manipulated in these programming languages. As your guide on this exciting journey, I’ll break down their similarities, highlight their differences, and sprinkle in some code examples to solidify the concepts.

The Object-Oriented Stage: Where Everything Plays a Role

Imagine a grand theater where everything is an actor — numbers, strings, even functions themselves. This is the essence of object-oriented programming, a paradigm embraced by both Python and JavaScript. In this theater, each actor (object) has its own set of properties (data) and methods (functions) that act upon that data. Let’s say you have an object representing a student. Its properties might include the student’s name, ID, and grades, while its methods could be functions to calculate grade averages or generate report cards.

Variable References: The Spotlight on the Actors (and Not the Props)

When you assign a value to a variable in Python or JavaScript, you’re not directly placing the value on center stage. Instead, you’re creating a reference, like a spotlight illuminating the actor. This means multiple variables can point to the same object, providing a dynamic way to manage data. Imagine multiple spotlights highlighting the same talented actor on stage — they all provide access to the same performer.

Dynamic Typing: Freedom with a Hint of Caution

Both Python and JavaScript are champions of dynamic typing. Unlike some stricter languages, you don’t need to announce a variable’s data type beforehand. The language infers the type based on the assigned value at runtime. This offers flexibility, but treat it with care! Assigning a string value to a variable meant for a number might lead to unexpected errors down the line.

Mutability Matters: When the Actors Can (or Can’t) Change Costumes

Some objects in our theatrical production are mutable, meaning they can change their properties after their grand entrance. In Python, lists are prime examples of mutable objects. You can modify elements within a list, allowing our student object’s list of grades to be updated as new assessments come in. However, strings in Python are immutable — trying to change a character within a string is like trying to rewrite a character’s line mid-performance; it just won’t work! JavaScript flips the script on mutability — strings are considered immutable, while arrays (similar to Python’s lists) are mutable.

Inheritance: Borrowing Traits for a Stellar Performance

Object-oriented programming allows objects to inherit properties and methods from others, creating a hierarchy of actors. Python utilizes classes for inheritance. Imagine a base class called Person that defines properties like name and age. A subclass called Student can inherit these properties from Person and add its own specific properties like student ID and major. In JavaScript, prototypes take center stage for inheritance. Each object has a prototype object from which it inherits properties. This prototype object can itself inherit from another prototype, creating a chain of inheritance.

Code Examples: Bringing Down the Curtain on Confusion

Let’s solidify these concepts with some code examples:

Dynamic Typing:


# Python
x = 5
print(type(x)) # Output: <class ‘int’>
x = “hello”
print(type(x)) # Output: <class ‘str’>


// JavaScript
let x = 5;
console.log(typeof x); // Output: number
x = “hello”;
console.log(typeof x); // Output: string



# Python: Mutable Lists
my_list = [1, 2, 3]my_list[0] = 10 # Modifying an element within the list
print(my_list) # Output: [10, 2, 3]# Python: Immutable Strings
name = “Alice”
# name[0] = “B” # This will cause a TypeError!


// JavaScript: Mutable Arrays
let my_array = [1, 2, 3];
my_array[0] = 10; # Modifying an element within the array
console.log(my_array); // Output: [10, 2, 3]

// JavaScript: Immutable Strings
let name = “Alice”;
// name[0] = “B” # This won’t work in JavaScript either!

Variable Scope: Where the Actors Take the Stage

Understanding variable scope is crucial. Python uses lexical scoping, meaning a variable’s availability is determined by its position within the code’s structure. Imagine a play with multiple acts. In Python, with lexical scoping, a variable declared within a function is only accessible within that function (like a prop stored backstage) or any nested functions within it. Variables declared outside of functions are considered globally scoped (props readily available to all actors throughout the play). Here’s an example:


# Python: Lexical Scoping
x = 10

def foo():
y = 20 # y is local to foo
print(x) # Can access outer x
print(y) # Can access local y


# print(y) # This would cause a NameError because y is local to foo

JavaScript, on the other hand, employs function scoping. A variable declared within a function is only accessible within that function and any functions nested within it. However, variables declared with var are accessible throughout the entire script (like permanent set pieces on stage), regardless of where they are declared within the script. This can lead to unintended consequences if not handled carefully. Here’s an example:


// JavaScript: Function Scoping
let x = 10; // Let’s use `let` for better practice

function foo() {
let y = 20; // y is local to foo
console.log(x); // Can access outer x
console.log(y); // Can access local y


// console.log(y); // This would cause a ReferenceError because y is local to foo

var global_var = “global”; // Declared with var, this is globally accessible

function bar() {
console.log(global_var); // Can access global_var from anywhere in the script


Special Methods: Behind the Scenes Magic

Both Python and JavaScript utilize special methods, also known as dunder methods (due to their double underscore notation), to define how objects behave in certain situations. These methods act like stage directions for the actors, dictating how they interact with each other and the environment. For instance, the __init__ method in Python is like a constructor, initializing an object’s properties when it’s created. The __add__ method defines how the + operator behaves for a custom object.

Containers: The Stage Crew and Props

Every play needs its crew and props! Similarly, Python and JavaScript provide built-in container types to hold and organize data. These containers, like lists, dictionaries, sets, and tuples, act as the storage units for various data pieces used throughout the program. They differ in their mutability (whether their contents can be changed) and ordering (how elements are arranged). Understanding these container types is vital for managing data effectively within your program.

Conclusion: Applause for the Data Model Understanding!

By grasping the similarities and differences between Python’s and JavaScript’s data models, you’ve taken a significant step towards becoming a confident programmer in either language. Remember, these data models are the foundation upon which you build your programs. The more you explore and experiment, the more comfortable you’ll become in manipulating data and bringing your programming visions to life!

Comparing the Data Models of Python and JavaScript: A Deep Dive was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

​ Level Up Coding – Medium

about Infinite Loop Digital

We support businesses by identifying requirements and helping clients integrate AI seamlessly into their operations.

Gartner Digital Workplace Summit Generative Al

GenAI sessions:

  • 4 Use Cases for Generative AI and ChatGPT in the Digital Workplace
  • How the Power of Generative AI Will Transform Knowledge Management
  • The Perils and Promises of Microsoft 365 Copilot
  • How to Be the Generative AI Champion Your CIO and Organization Need
  • How to Shift Organizational Culture Today to Embrace Generative AI Tomorrow
  • Mitigate the Risks of Generative AI by Enhancing Your Information Governance
  • Cultivate Essential Skills for Collaborating With Artificial Intelligence
  • Ask the Expert: Microsoft 365 Copilot
  • Generative AI Across Digital Workplace Markets
10 – 11 June 2024

London, U.K.