avatarLaxfed Paulacy

Summarize

PYTHON — More JavaScript Syntax Python

Programming isn’t about what you know; it’s about what you can figure out. — Chris Pine

PYTHON — Built-In Dictionary in Python

## More JavaScript Syntax: Python

This lesson covers various aspects of JavaScript syntax and compares them with Python. It delves into JavaScript features such as enumerations, arrow functions, default function parameters, labeled statements, and more. It also discusses the concept of iterators, generators, and asynchronous functions.

JavaScript Enumerations

JavaScript does not have an enumeration, but you can mimic the concept by freezing an object. Freezing an object prevents adding, removing, or altering its attributes. This is different from the const keyword, which prevents changing what a variable points to.

const user = {
  name: 'John',
  age: 30
};

Object.freeze(user);

user.name = 'Jane';  # This will not have any effect

Arrow Functions

Arrow functions are a more concise way of writing functions in JavaScript. They have some subtle differences with scoping and the this keyword.

# Using the function keyword
function add(a, b) {
  return a + b;
}

# Using arrow function
const add = (a, b) => a + b;

Default Function Parameters

JavaScript functions support default values and a variable number of parameters.

function greet(name = 'Stranger') {
  return `Hello, ${name}!`;
}

console.log(greet());  # Output: Hello, Stranger!
console.log(greet('John'));  # Output: Hello, John!

Generators and Iterators

JavaScript introduced iterables, iterators, and generators. Generators are a special kind of function that returns a suspended generator object. It uses the yield keyword to suspend the generator and return a value.

function* generateValues() {
  yield 1;
  yield 2;
  yield 3;
}

const gen = generateValues();
console.log(gen.next().value);  # Output: 1
console.log(gen.next().value);  # Output: 2

Asynchronous Functions

JavaScript supports asynchronous functions, declared using the async keyword. When called, an asynchronous function returns a promise object that can be resolved using the await keyword.

async function fetchData() {
  let result = await fetch('https://api.example.com/data');
  return result.json();
}

fetchData().then(data => console.log(data));

These are some of the key differences and features of JavaScript syntax compared to Python. Understanding these syntax elements is crucial for Python developers working with JavaScript.

In summary, JavaScript has its own unique syntax and features that differ from Python. It’s important to be aware of these differences when working with both languages.

PYTHON — Collect User Input in Python

ChatGPT
Syntax
Python
JavaScript
Recommended from ReadMedium