Variables, Data Types, and Basic Operators
Welcome to this lesson on variables, data types, and basic operators in JavaScript! As a Java developer, you’ll find some familiar concepts here, but also some key differences that make JavaScript unique. Let’s dive in and explore how these fundamental building blocks work in JavaScript.
Variable Declaration
In JavaScript, we have three ways to declare variables: var
, let
, and const
. This is quite different from Java, where we typically use the type name followed by the variable name.
// JavaScript
var oldStyle = "I'm old school";
let modernVar = "I'm block-scoped";
const constant = "I can't be reassigned";
// Java
String javaVar = "I'm statically typed";
The let
and const
keywords were introduced in ES6 (ECMAScript 2015) and are now the preferred way to declare variables. let
allows you to declare block-scoped variables that can be reassigned, while const
is used for variables that won’t be reassigned.
Dynamic Typing
One of the biggest differences you’ll encounter coming from Java is that JavaScript is dynamically typed. This means you don’t need to specify the type of a variable when you declare it, and you can change its type later.
// JavaScript
let x = 5; // x is a number
x = "five"; // Now x is a string
This flexibility can be powerful, but it also means you need to be more careful about type-related errors.
Primitive Data Types
JavaScript has the following primitive data types:
number
: Represents both integer and floating-point numbersstring
: Represents textual databoolean
: true or falsenull
: Intentional absence of any object valueundefined
: Represents a variable that has been declared but not assigned a value
// JavaScript
let num = 42;
let text = "Hello, World!";
let isTrue = true;
let empty = null;
let notDefined;
console.log(typeof num); // "number"
console.log(typeof text); // "string"
console.log(typeof isTrue); // "boolean"
console.log(typeof empty); // "object" (this is a known quirk in JavaScript)
console.log(typeof notDefined); // "undefined"
Note that unlike Java, JavaScript doesn’t have separate types for integers and floating-point numbers - they’re all just number
.
Object and Symbol Types
In addition to primitives, JavaScript has the object
type, which is a collection of properties, and the symbol
type, which was introduced in ES6 for creating unique identifiers.
// JavaScript
let person = {name: "Alice", age: 30};
let id = Symbol("id");
Type Coercion and Equality Operators
JavaScript performs automatic type coercion in certain situations. This can lead to unexpected results if you’re not careful. To handle this, JavaScript has two sets of equality operators:
==
and!=
: These perform type coercion before comparison===
and!==
: These compare both value and type without coercion
// JavaScript
console.log(5 == "5"); // true
console.log(5 === "5"); // false
As a best practice, it’s generally recommended to use ===
and !==
to avoid unexpected type coercion.
Arithmetic and Logical Operators
Arithmetic and logical operators in JavaScript are similar to those in Java:
// JavaScript
let a = 10, b = 3;
console.log(a + b); // Addition: 13
console.log(a - b); // Subtraction: 7
console.log(a * b); // Multiplication: 30
console.log(a / b); // Division: 3.3333...
console.log(a % b); // Modulus: 1
console.log(a ** b); // Exponentiation: 1000 (Not available in Java)
console.log(a > b); // Greater than: true
console.log(a < b); // Less than: false
console.log(a >= b); // Greater than or equal to: true
console.log(a <= b); // Less than or equal to: false
console.log(true && false); // Logical AND: false
console.log(true || false); // Logical OR: true
console.log(!true); // Logical NOT: false
Note the **
operator for exponentiation, which is not present in Java.
Conclusion
In this lesson, we’ve covered the basics of variables, data types, and operators in JavaScript. We’ve seen how JavaScript’s dynamic typing differs from Java’s static typing, explored the various data types available, and looked at how operators work in JavaScript.
In the next lesson, we’ll dive into control structures and functions in JavaScript, where you’ll see both familiar constructs and some JavaScript-specific features that can make your code more expressive and concise. We’ll explore how JavaScript’s approach to these fundamental programming concepts differs from what you’re used to in Java, and how you can leverage these differences to write more flexible and powerful code.