Getting Started with JavaScript

Welcome to the world of JavaScript! In this lesson, we’ll set up your JavaScript environment and introduce you to the core concepts of the language. As a Python developer, you’ll find some familiar concepts and some new ones to explore.

Setting Up Your JavaScript Environment

Unlike Python, JavaScript was originally designed to run in web browsers. However, we can also run it outside the browser using Node.js. Let’s set up your environment:

  1. Install Node.js. This allows you to run JavaScript on your computer, similar to how you run Python scripts.

  2. Open your browser’s console for quick experiments:

    • In Chrome: Right-click > Inspect > Console
    • In Firefox: Right-click > Inspect Element > Console
  3. Set up a code editor. We recommend Visual Studio Code with these extensions:

    • ESLint for linting
    • Prettier for code formatting

Introduction to JavaScript

JavaScript, created in 1995, has evolved from a simple scripting language to a powerful, multi-paradigm programming language. While Python is a general-purpose language, JavaScript’s primary domain is web development, though it has expanded to server-side programming with Node.js.

Key differences from Python:

  1. JavaScript uses curly braces {} to define blocks, not indentation.
  2. Semicolons ; are used to end statements (though they’re often optional).
  3. JavaScript has more flexible typing rules compared to Python.

Let’s write our first JavaScript program:

// This is a comment in JavaScript
console.log("Hello, JavaScript!");

To run this in Node.js, save it as hello.js and run:

node hello.js

You’ll see:

Hello, JavaScript!

Brief Comparison with Python

Here’s a quick comparison of a simple function in Python and JavaScript:

Python:

# Python function
def greet(name):
    return f"Hello, {name}!"

print(greet("Python Developer"))

JavaScript:

// JavaScript function
function greet(name) {
    return `Hello, ${name}!`;
}

console.log(greet("Python Developer"));

Notice the similarities in structure, but differences in syntax. JavaScript uses function keyword, curly braces, and semicolons.

Conclusion

In this lesson, we’ve set up your JavaScript environment and introduced you to the basic structure of JavaScript code. We’ve seen how it compares to Python in some simple examples.

In the next lesson, “Variables, Data Types, and Basic Operators”, we’ll dive deeper into JavaScript’s variable declarations, data types, and how they compare to what you’re familiar with in Python. We’ll explore JavaScript’s dynamic typing system and how it differs from Python’s approach.