OPERATOR PADA JAVASCRIPT : Logika, String, Typeof dan Kondisional

3 min read 3 hours ago
Published on Apr 04, 2026 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will explore various operators in JavaScript, specifically focusing on logical operators, string concatenation, the typeof operator, and conditional (ternary) operators. Understanding these concepts is crucial for writing effective JavaScript code and enhancing your programming skills.

Step 1: Understanding Logical Operators

Logical operators are used to combine multiple boolean expressions. In JavaScript, the most common logical operators are AND, OR, and NOT.

  • AND (&&): Returns true if both operands are true.
  • OR (||): Returns true if at least one operand is true.
  • NOT (!): Reverses the boolean value of the operand.

Practical Tips

  • Use logical operators to control the flow of your program based on multiple conditions.
  • Example:
    let a = true;
    let b = false;
    console.log(a && b); // Outputs: false
    console.log(a || b); // Outputs: true
    console.log(!a);     // Outputs: false
    

Step 2: String Concatenation

String concatenation allows you to join two or more strings together. In JavaScript, you can use the + operator or template literals.

Using the + Operator

  • Example:
    let firstName = "John";
    let lastName = "Doe";
    let fullName = firstName + " " + lastName;
    console.log(fullName); // Outputs: "John Doe"
    

Using Template Literals

  • Template literals are enclosed by backticks (`) and allow for easier string interpolation.
  • Example:
    let fullName = `${firstName} ${lastName}`;
    console.log(fullName); // Outputs: "John Doe"
    

Practical Tips

  • Template literals are useful for including variables directly in strings without needing to concatenate them.

Step 3: Using the Typeof Operator

The typeof operator is a unary operator that returns the type of a variable or expression.

Example Usage

  • Example:
    console.log(typeof "Hello"); // Outputs: string
    console.log(typeof 42);       // Outputs: number
    console.log(typeof true);      // Outputs: boolean
    console.log(typeof undefined); // Outputs: undefined
    

Practical Tips

  • Use typeof to check variable types before performing operations that depend on specific types.

Step 4: Implementing Conditional (Ternary) Operator

The conditional operator, also known as the ternary operator, is a shorthand way to write an if-else statement.

Syntax

condition ? exprIfTrue : exprIfFalse;

Example Usage

  • Example:
    let age = 18;
    let canVote = (age >= 18) ? "Yes, you can vote." : "No, you cannot vote.";
    console.log(canVote); // Outputs: "Yes, you can vote."
    

Practical Tips

  • The ternary operator is useful for making code more concise, but use it judiciously for readability.

Conclusion

In this tutorial, we covered the essential operators in JavaScript, including logical operators, string concatenation techniques, the typeof operator, and the conditional (ternary) operator. Mastering these operators will significantly enhance your coding efficiency and problem-solving abilities.

Next Steps

  • Practice using these operators in your own JavaScript projects.
  • Explore more advanced topics such as error handling and asynchronous programming to further improve your skills. Happy coding!