Cookie Consent by Free Privacy Policy Generator 📌 Learning Javascript as a Python Developer

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Learning Javascript as a Python Developer


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

My previous post Learn Python as a Javascript developer was so popular I decided to write the same thing but reversed.

As previously stated, I'm a Javascript and Python fullstack developer and like most self-taught developers, I started by learning HTML, CSS and Javascript first.

When I learned Python it was pretty easy as it is less restrictive than javascript. Python is great for handling backend data and creating simple scripts like web-scrapers.

Python is commonly associated with data analysis and javascript with web development. However, the introduction of Node JS and the popularity of javascript amongst developers means you should probably learn it. Even just being able to read it would serve you well.

If you're nervous about learning Javascript, remember that Javascript is a high level programming language. This means it is around the same level of difficulty to learn as Python.

In this tutorial, I'll outline the main differences between the two coding languages.

Set Up

If you're starting out, I recommend using a sandbox to test your code. Run JS is a very useful tool as it logs

Methods

The methods used in python and javascript are named differently.

Here are some examples of this:

In python:

len("Hello world") # Length of a string
arr.append("new string") # Add new item to end of list
arr.sort() # Sort a list

In javascript:

"Hello World".length; // Length of a string
arr.push("new string"); // Add new item to end of array
arr.sort((a, b) => a - b); // Sort an array

As you can see, the differences aren't huge and you of course don't need to remember all of them.

Here are some useful links for you to reference:

Type Page
Strings Methods
List/Array Methods
Build in Functions Keywords

Output

When you want to print something in the console. Just change print to console.log!

In python:

print("hello world")

In javascript:

console.log("hello world");

Comments

When leaving a comment or commenting something out change to # to //:

In python:

"""python comment"""

# Multi
# Line
# Comment

In javascript:

// javascript comment

/* multiline
javascript
comment */

Variables

Variables is where the difference really begin to show. For those new to Javascript, it may take some time to adjust.

Here are the main differences.

  • Javascript use camel casing and Python use snake casing.
  • Cases for python , const, let and var for javascript.

In Python:

my_example = "hello world" # basic variable
MY_EXAMPLE = "hello world" # Red flag to not change

In Javascript:

// Any of these 3 can be used.
var myExample = "hello world";
let myExample = "hello world";
const myExample = "hello world";

Let's quickly break is down.

  • var was used in all JavaScript code from 1995 to 2015, older browsers still only use var.
  • The let and const keywords were added to JavaScript in 2015.

The general rule is declare variables with const, UNLESS you think the value of the variable will change - in that case use let.

Here is an example:

let valOne = "hello javascript";
const valTwo = "This is javascript";

valOne = "hello world";
valTwo = "This is now new";

// valOne is now "hello world"
// valTwo is still "This is javascript"

The second value remains because it was declared using const

List and Arrays

Lists in Javascript are referred to as arrays, similar to variables it must be declared with a const or let

In Python:

my_list = [1, 3, 5]

In Javascript:

const myList = [4, 5, 6];

Functions

Python uses def followed by a colon : for their functions:

def foo(str):
    print(str)

Javascript uses curly braces {} and function (or an arrow function):

function foo(str) {
  console.log(str);
}
// OR an Arrow Function

const foo = (str) => console.log(str);

Unlike python, javascript is not a language which changes due to indentation. Although it is highly recommended that you indent your js code for readability, it is not a deal breaker and it won't stop your function from working.

If you use an arrow function, it must come before it's usage. This does rule not apply a regular javascript function.

arrTestFunc();
// will throw an error saying "arrTestFunc" has not been declared.
regTestFunc();
// 'hello world' will be consoled'

const arrTestFunc = () => console.log("hello world");

function regTestFunc() {
  console.log("hello world");
}

If Statements

In Python just uses colons : after each condition and an indented code block. Statements are separated by a elif and else:

if condition_one:
  print("condition 1 met")
elif condition_two:
  print("condition 2 met")
else:
  print('else condition')

In Javascript wrap the condition in parenthesis () and the output for the condition in curly braces {}. Each condition can be separated with else if or else:

if (condition1) {
  console.log("condition 1 met");
} else if (condition2) {
  console.log("condition 2 met");
} else {
  console.log("else condition");
}

Logical Operators

When declaring conditions, we use the following:

Python Javascript
or ||
and &&
not !condition

In Python:

if condition1 or condition2 # ||
if condition1 and condition2 # &&
if not condition ## !condition

In Javascript:

if (condition1 || condition2) // or
if (condition1 && condition2) // and
if (!condition) // is not

Experiment with this in a sandbox as it can be confusing at times not using or, and and not.

For Loop

In Python we replace that logic with in range(x).

In Javascript, you are required to declare a variable and increment it using the for loop.

in Python:

for i in range(5):
    print(i)

In Javascript:

for (let i = 0; i < 5; i++) {
  console.log(i);
}

If you want to access an object or item in an Array:

In Python:

for obj in arr:
    print(obj)

In Javascript:

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

In this instance we're logging each item in array (list) by accessing each index starting at 0.

Types

Primitive data types represent the fundamental values that we can work with in a programming language. JavaScript has 6 types to Python's has 4.

  • Python data types: Integers (int), Floats (float), Booleans (bool), and strings (str).
  • JavaScript data types: undefined, Boolean, String, Number, BigInt, and Symbol.

To check types in javascript:

type(instance);

To check types in python:

typeof instance;

Imports

Importing files are pretty similar, you just have to swap the order of import and from.

In Python:

from django.db import models

In Javascript:

import React from "react";

One of the main difference is that you must export things in javascript, this is not automatic like python.

You can either declare a default export or export multiple things from one file as an object.

Here is an example:

// firstFile.js - export 2 functions

export const testFunc = () => console.log("hello world");
export function secondTestFunc() {
  console.log("hello world 2");
}

// secondFile.js - export 1 function by default
const uniqueFunc = () => console.log("hello world from another file");

export default uniqueFunc;

// index.js -

import { testFunc, secondTestFunc } from "firstFile.js";
import uniqueFunc from "secondFile.js";

Note: To avoid confusion, I imported named the import uniqueFunc from the secondFile but you name the import whatever you wish as it is the default import.

Classes

In Python, the constructor that initialises the new instance is called __init__. This method is called automatically when an instance of the class is created to initialise its attributes. It's parameters list defines the values that we have to pass to create the instance. This list starts with self as the first parameter.

In JavaScript, the constructor method is called constructor and it has a parameters list as well. Javascript uses curly braces {} and Python uses colons :.

Class format

In Python:

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

In Javascript:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

Assign value to a Class

In Python:

self.attribute = value

In Javascript:

this.attribute = value;

Create Class Instance

In Python:

person_one = Person("John", 18)

In Javascript:

personOne = new Person("John", 18);

Summary

The honest truth is learning javascript as a python developer is harder the other way round. But if you have a good level of python, all you need to do is wrap your head around the usage as the logic is pretty much the same. The goal of this tutorial is to show you the subtle differences between python and javascript. The best way to learn is practice. Play around with a sandbox and write some basic code in javascript.

A good way to practice is trying solve a few problems in Javascript on Code Wars.

Thanks for reading and Good Luck on your coding journey!

...



📌 Learning Javascript as a Python Developer


📈 28.49 Punkte

📌 Day-10 of Learning JavaScript: Whip Up Weather Wonders: Craft Your Own City-Ready Weather App with JavaScript Magic!


📈 22.15 Punkte

📌 Is learning Linux the same as learning Bash? And if not, what does learning Linux consist of?


📈 20.98 Punkte

📌 heise-Angebot: Machine Learning mit Python – KI und Deep Learning in 5 Webinaren erklärt


📈 20.55 Punkte

📌 heise-Angebot: Machine Learning mit Python – KI und Deep Learning in 5 Webinaren erklärt


📈 20.55 Punkte

📌 heise-Angebot: Last Call: Machine Learning mit Python – KI und Deep Learning in 5 Webinaren


📈 20.55 Punkte

📌 DATA SCIENCE, DEEP LEARNING, & MACHINE LEARNING WITH PYTHON


📈 20.55 Punkte

📌 5 Python Tricks Every Python Developer should know


📈 20.48 Punkte

📌 Python for Newbies: Your Ultimate Guide to Learning Python and Best Practices


📈 20.13 Punkte

📌 Python for Newbies: Your Ultimate Guide to Learning Python and Best Practices


📈 20.13 Punkte

📌 Python Curriculum 2024 Upgrade – Interactive Python Learning in your Browser


📈 20.13 Punkte

📌 PYTHON 3 BOOTCAMP: DEEP LEARNING INTO PYTHON 3 WITH GUIS


📈 20.13 Punkte

📌 Any books similar to Black Hat Python, or Violent Python that use Python v3?


📈 19.71 Punkte

📌 What Makes Python Python? (aka Everything About Python’s Grammar)


📈 19.71 Punkte

📌 Pufferüberlauf in python-crcmod, python-cryptography und python-cryptography-vectors (SUSE)


📈 19.71 Punkte

📌 Switching Geany to execute Python files as Python 3, not Python 2


📈 19.71 Punkte

📌 Python for Beginners [1 of 44] Programming with Python | Python for Beginners


📈 19.71 Punkte

📌 Introducing More Python for Beginners | More Python for Beginners [1 of 20] | More Python for Beginners


📈 19.71 Punkte

📌 V8 Internals for JavaScript Developers (Make Your JavaScript Faster)


📈 15.16 Punkte

📌 JavaScript: Dirty Parts of the Language (aka Maneuvering JavaScript Errors)


📈 15.16 Punkte

📌 JavaScript: Dirty Parts of the Language (aka Maneuvering JavaScript Errors)


📈 15.16 Punkte

📌 A Brief History of JavaScript by the Creator of JavaScript


📈 15.16 Punkte

📌 Why Is JavaScript So Fast? (aka JavaScript Engines - How Do They Even?)


📈 15.16 Punkte

📌 Dynamic Rendering for JavaScript web apps - JavaScript SEO


📈 15.16 Punkte

📌 Top 10 JavaScript Vulnerabilities (aka OWASP Top 10 for JavaScript Developers)


📈 15.16 Punkte

📌 Javascript: Neue Facebook-Webseite setzt komplett auf Javascript


📈 15.16 Punkte

📌 Low CVE-2020-14063: Tc custom javascript project Tc custom javascript


📈 15.16 Punkte

📌 HTML and Javascript Teacher - Code examples in HTML and Javascript.


📈 15.16 Punkte

📌 Demo: Data types in JavaScript [16 of 51] | Beginner's Series to JavaScript


📈 15.16 Punkte

📌 Demo: Objects in JavaScript [44 of 51] | Beginner's Series to JavaScript


📈 15.16 Punkte

📌 Running JavaScript: browser or server [3 of 51] | Beginner's Series to JavaScript


📈 15.16 Punkte

📌 Mozilla Firefox up to 13.0 JavaScript SandBox Utility javascript: URL memory corruption


📈 15.16 Punkte











matomo