Variables, Data Types, and Basic Operators

In this lesson, we’ll explore how JavaScript handles variables, data types, and basic operators, drawing comparisons with Python to help you understand the similarities and differences between the two languages.

Variables

Unlike Python, JavaScript requires explicit variable declaration. Let’s look at the three ways to declare variables in JavaScript:

// JavaScript
var oldWay = "I'm using var";
let modernWay = "I'm using let";
const constant = "I'm a constant";

In Python, you might be used to:

# Python
variable = "I'm a variable"

The let keyword is similar to Python’s implicit declaration, while const is used for values that shouldn’t be reassigned (similar to uppercase convention in Python for constants).

Data Types

JavaScript, like Python, is dynamically typed. However, it has some key differences:

// JavaScript
let number = 42;
let float = 3.14;
let string = "Hello, World!";
let boolean = true;
let nullValue = null;
let undefinedValue;
let array = [1, 2, 3];
let object = { key: "value" };

In Python, you might be familiar with:

# Python
number = 42
float_num = 3.14
string = "Hello, World!"
boolean = True
none_value = None
list = [1, 2, 3]
dictionary = {"key": "value"}

Note that JavaScript doesn’t distinguish between integers and floats like Python does. Also, JavaScript has both null and undefined, while Python only has None.

Type Coercion

JavaScript performs automatic type coercion, which can lead to unexpected results:

// JavaScript
console.log("5" + 3);  // Outputs: "53"
console.log("5" - 3);  // Outputs: 2

Python, on the other hand, is more strict:

# Python
print("5" + 3)  # Raises TypeError
print(int("5") - 3)  # Outputs: 2

Basic Operators

Many operators in JavaScript work similarly to Python:

// JavaScript
let a = 5;
let b = 2;

console.log(a + b);  // Addition: 7
console.log(a - b);  // Subtraction: 3
console.log(a * b);  // Multiplication: 10
console.log(a / b);  // Division: 2.5
console.log(a % b);  // Modulus: 1
console.log(a ** b); // Exponentiation: 25

console.log(a === b);  // Strict equality: false
console.log(a !== b);  // Strict inequality: true
console.log(a > b);    // Greater than: true

console.log(true && false);  // Logical AND: false
console.log(true || false);  // Logical OR: true
console.log(!true);          // Logical NOT: false

The main differences from Python are:

  1. JavaScript uses === for strict equality (type and value), while == allows type coercion.
  2. The logical operators are &&, ||, and ! instead of and, or, and not.
  3. JavaScript has a typeof operator to check types:
// JavaScript
console.log(typeof 42);  // Outputs: "number"
console.log(typeof "Hello");  // Outputs: "string"

Conclusion

In this lesson, we’ve covered the basics of variables, data types, and operators in JavaScript, highlighting the key differences from Python. Understanding these fundamental concepts is crucial as we move forward with more complex JavaScript features.

In the next lesson, we’ll explore control structures and functions in JavaScript, building on what we’ve learned here about variables and operators. We’ll see how JavaScript’s syntax for conditionals and loops differs from Python’s, and discover the versatility of JavaScript functions.