Operators in JavaScript Fundamentals

StableShark avatar
StableShark
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the output of the following code snippet: console.log(5 < 10)?

true

What does the following code snippet output: console.log(3 > 5 || 0)?

true

In JavaScript, what would be the result of console.log(5 === '5')?

false

What is the final value of a after executing the code: let a = 5; a++; ++a; a--?

7

What is the output of console.log(!(5 > 3))?

'false'

What would be the result of console.log(1 == true)?

'Syntax Error'

Given a = 2, what does console.log(a++ - --a) output?

-2

What will be the output of console.log((4 + 2) * 3)?

'18'

For console.log(!(0 && true)), what would be the output?

'true'

If let x = 5, what will be the result of console.log(x += x - x++)?

'0'

Study Notes

Prefix Increment and Decrement Operators

  • The operator is placed before the variable, and the value of the variable is incremented or decremented before it is used.
  • Example: let a = 10; console.log(++a); // 11 and let a = 10; console.log(--a); // 9

Postfix Increment and Decrement Operators

  • The operator is placed after the variable, and the value of the variable is used before it is incremented or decremented.
  • Example: let a = 10; console.log(a++); // 10 and let a = 10; console.log(a--); // 10

Comparison Operators

  • Comparison operators compare two values and return a Boolean value: either true or false.
  • They are useful in decision-making and loop programs in JavaScript.

Arithmetic Operators

  • Addition: let sum = 5 + 3; console.log(sum); // 8
  • Subtraction: let difference = 10 - 4; console.log(difference); // 6
  • Multiplication: let product = 2 * 6; console.log(product); // 12
  • Division: let quotient = 15 / 3; console.log(quotient); // 5
  • Remainder (Modulus): let remainder = 17 % 4; console.log(remainder); // 1
  • Exponentiation: let result = 2 ** 4; console.log(result); // 16

Assignment Operator

  • Assignment operators are used to assign values to variables.
  • The = sign is used for assignment.
  • Examples:
    • Addition assignment: x += 3;
    • Subtraction assignment: x -= 2;
    • Multiplication assignment: x *= 4;
    • Division assignment: x /= 3;
    • Remainder (Modulus) assignment: x %= 5;
    • Exponentiation assignment: x **= 2;

Logical Operators

  • Logical OR: true || true; // true, true || false; // true, false || true; // true, false || false; // false
  • Logical NOT: ! converts the operator to Boolean and returns a flipped value.
  • Example: let Yes = true; let No = false; console.log(!Yes); // false, console.log(!No); // true

Operator Precedence

  • Operator precedence determines the order in which operators are parsed concerning each other.
  • Example: let result = 2 + 3 * 4; console.log(result); // 14
  • The order of operations is determined by the operator's precedence value.

Operator Associativity

  • Operator associativity defines the order in which operators of the same precedence are evaluated.

Learn about the prefix and postfix increment and decrement operators in JavaScript. Understand how these operators behave before and after the variable. Practice examples to see the effects of using these operators.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser