Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Arithmetic Operators in JavaScript

  • Arithmetic operators are symbols or keywords used to perform mathematical calculations and operations on numerical values

  • They allow the computer to perform addition, subtraction, multiplication, division, and more

Addition operator (+)

  • The addition operator (+) is used to add numerical values together or concatenate strings:

const sum = 5 + 3;

const fullName = 'John' + ' ' + 'Doe';

  • In this example, the addition operator adds the values 5 and 3, resulting in 8

  • It also concatenates the strings 'John', ' ', and 'Doe' to form the full name 'John Doe'

Subtraction operator (-)

  • The subtraction operator (-) is used to subtract one numerical value from another:

const difference = 10 - 4;

  • In this example, the subtraction operator subtracts the value 4 from 10, resulting in 6

Multiplication operator (*)

  • The multiplication operator (*) is used to multiply numerical values together:

const product = 3 * 5; 

  • In this example, the multiplication operator multiplies the values 3 and 5, resulting in 15

Exponentiation operator (**)

  • The exponentiation operator (**) is used to multiply numerical values by the power of another number:

const product = 3 ** 3; 

  • In this example, the exponentiation operator multiplies the value 3 to the power 3, resulting in 27

The division operator (/)

  • The division operator (/) is used to divide one numerical value by another:

const quotient = 10 / 2;

  • In this example, the division operator divides the value 10 by 2, resulting in 5

The modulus operator (%)

  • The modulus operator (%) returns the remainder when one numerical value is divided by another:

const remainder = 10 % 3;

  • In this example, the modulus operator divides the value 10 by 3 and returns the remainder, which is 1

Increment operator (++) & decrement operator (–)

  • The increment operator (++) and decrement operator (--) are used to increase or decrease the value of a numerical variable by one:

let counter = 5;
counter++;
counter--;

  • In this example, the increment operator increases the value of counter by one, resulting in 6

  • The decrement operator decreases it back to 5

Operator precedence (BIDMAS / BODMAS)

  • Arithmetic operators follow the rules of operator precedence, which determine the order in which operators are evaluated

  • Parentheses () can be used to control the order of evaluation:

const result = 2 + 3 * 4; 
const adjustedResult = (2 + 3) * 4; 

  • In the first example, without parentheses, the multiplication (*) is performed before the addition (+), resulting in 14

  • In the second example, with parentheses, the addition is evaluated first, resulting in 5, which is then multiplied by 4, resulting in 20

Logical Operators in JavaScript

  • Logical or comparison operators are symbols or keywords used to compare values and return a boolean result

  • They allow a comparison of variables, or expressions to determine relationships, equality, or inequality between them.

Equal to (==) operator

  • The equal to the operator (==) compares two values and returns true if they are equal, and false otherwise

  • It performs type coercion, meaning it converts the operands to a common type before comparing them:

const x = 5;
const y = '5';

console.log(x == y); 

  • In this example, the equal to operator compares the value 5 (a number) with the value '5' (a string). Since JavaScript performs type coercion, the operands are considered equal, resulting in true

Not equal to (!=) operator

  • The not equal to the operator (!=) compares two values and returns true if they are not equal, and false if they are equal

  • Like the equal to operator, it performs type coercion:

const x = 5;
const y = '5';

console.log(x != y);

  • In this example, the not equal to operator compares the value 5 with the value '5'. Since JavaScript performs type coercion and considers the operands equal, the result is false

Strict equal to (===) operator

The strict equal to the operator (===) compares two values and returns true if they are equal in both value and type. Unlike the equal to operator, it does not perform type coercion:

const x = 5;
const y = '5';

console.log(x === y); 

  • In this example, the strict equal to operator compares the value 5 with the value '5'. Since the operands have different types, the result is false

Strict not equal to (!==) operator

  • The strict is not equal to the operator (!==) compares two values and returns true if they are not equal in either value or type

  • It does not perform type coercion:

const x = 5;
const y = '5';

console.log(x !== y);

  • In this example, the strict not equal to the operator compares the value 5 with the value '5'. Since the operands have different types, the result is true

Greater than (>) & less than (<) operators

  • The greater than the operator (>) compares two values and returns true if the left operand is greater than the right operand

  • The less than operator (<) returns true if the left operand is less than the right operand:

const x = 5;
const y = 10;

console.log(x > y); 
console.log(x < y); 

  • In this example, the greater than operator compares the value 5 with the value 10, resulting in false

  • The less than operator compares 5 with 10, resulting in true

Greater than or equal to (>=) & less than or equal to (<=) operators

  • The greater than or equal to the operator (>=) compares two values and returns true if the left operand is greater than or equal to the right operand

  • The less than or equal to the operator (<=) returns true if the left operand is less than or equal to the right operand:

const x = 5;
const y = 10;

console.log(x >= y);
console.log(x <= y);

  • In this example, the greater than or equal operator compares 5 with 10, resulting in false

  • The less than or equal to operator compares 5 with 10, resulting in true

Boolean Operators in JavaScript

  • Boolean operators are symbols or keywords used to combine and manipulate boolean values

  • They allow the computer to perform logical operations, such as combining conditions, negating values, and determining whether an expression is true or false.

AND operator (&&)

  • The AND operator (&&) returns true if both operands are true, otherwise returns false:

const x = 5;
const y = 10;
const z = 15;

const result = (x < y) && (y < z);

  • In this example, the expression (x < y) && (y < z) evaluates to true because both conditions are true. If any of the conditions were false, the result would be false

OR operator (||)

  • The OR operator (||) returns true if at least one of the operands is true, and false if both operands are false:

const a = 5;
const b = 10;
const c = 15;

const result = (a > b) || (b < c);

  • In this example, the expression (a > b) || (b < c) evaluates to true because the second condition is true, even though the first condition is false. If both conditions were false, the result would be false

NOT operator (!)

  • The NOT operator (!) is used to negate a Boolean value. It returns true if the operand is false, and false if the operand is true:

const value = false;

const result = !value;

  • In this example, the ! operator negates the value of false, resulting in true

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注