JavaScript Basics and Operations: Data Types and Operators

Robert MuendoRobert Muendo
4 min read

Introduction

JavaScript is a versatile programming language, and understanding its basics is crucial for building interactive applications. In this blog, we'll cover JavaScript's data types and operators, along with simple examples to make learning fun and practical.

Understanding Data Types in JavaScript

Data types define the kind of values a variable can hold. JavaScript has several built-in data types, categorized as primitive and non-primitive.

Primitive Data Types

These data types hold single values and are immutable (cannot be changed).

  • String - Represents text enclosed in quotes.
let name = "John"; console.log(name); // Output: John
  • Number - Represents integers and floating-point numbers.
let age = 25; let pi = 3.14; console.log(age, pi); // Output: 25 3.14
  • Boolean - Represents true or false values.
let isStudent = true; console.log(isStudent); // Output: true
  • Undefined - A variable that has been declared but not assigned a value.
let x; console.log(x); // Output: undefined
  • Null - Represents an empty or unknown value.
let y = null; console.log(y); // Output: null
  • Symbol (ES6) - Used for creating unique identifiers.
let symbol1 = Symbol("unique"); console.log(symbol1); // Output: Symbol(unique)

Non-Primitive Data Types

These data types hold collections of values or references.

  • Object - Used to store key-value pairs.
let person = { name: "Alice", age: 30 }; console.log(person.name); // Output: Alice
  • Array - A collection of values.
let colors = ["Red", "Green", "Blue"]; console.log(colors[0]); // Output: Red

Operators in JavaScript

Operators allow you to perform operations on variables and values. They are categorized into different types.

Arithmetic Operators

These operators perform mathematical operations.

let a = 10, b = 5;
console.log(a + b); // Addition: 15
console.log(a - b); // Subtraction: 5
console.log(a * b); // Multiplication: 50
console.log(a / b); // Division: 2
console.log(a % b); // Modulus (remainder): 0
console.log(++a); // Increment: 11
console.log(--b); // Decrement: 4

Comparison Operators

These operators compare values and return true or false.

console.log(5 == "5"); // true (loose equality, type conversion happens)
console.log(5 === "5"); // false (strict equality, no type conversion)
console.log(10 != 5); // true
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 >= 10); // true
console.log(10 <= 9); // false

Logical Operators

Used for boolean logic operations.

let x = true, y = false;
console.log(x && y); // AND: false
console.log(x || y); // OR: true
console.log(!x); // NOT: false

Assignment Operators

Used to assign values to variables.

let num = 10;
num += 5; // num = num + 5
console.log(num); // Output: 15

String Operators

Used to concatenate (combine) strings.

let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName); // Output: John Doe

Type Coercion in JavaScript

JavaScript is loosely typed, meaning it automatically converts data types in certain operations.

console.log("5" + 2); // Output: "52" (number 2 is converted to string)
console.log("5" - 2); // Output: 3 (string "5" is converted to number)
console.log(true + 1); // Output: 2 (true is converted to 1)

Did You Know? JavaScript Quirks

Here are some interesting and sometimes confusing aspects of JavaScript:

  • typeof null is "object" (a long-standing bug in JavaScript).
console.log(typeof null); // Output: "object"
  • NaN (Not-a-Number) is a number type.
console.log(typeof NaN); // Output: "number"
  • JavaScript allows adding arrays to numbers.
console.log([1, 2, 3] + 4); // Output: "1,2,34" (array is converted to a string)

Conclusion

Understanding data types and operators is essential to mastering JavaScript. In the next blog, we'll explore control structures like loops and conditional statements to add logic to our code.

Have any questions or feedback? Drop them in the comments below!

What's Next?

In the next blog, we'll dive into Control Flow in JavaScript, exploring loops and conditions. We'll also build a guess-the-number game to apply these concepts in a fun, interactive way. Stay tuned for flowcharts and diagrams to visualize control flow effectively! ๐Ÿš€

10
Subscribe to my newsletter

Read articles from Robert Muendo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Robert Muendo
Robert Muendo