Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1448

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T08:15:09+00:00 2024-11-25T08:15:09+00:00

JavaScript Operators Reference

  • 62k

As you may know, operators are used to perform specific mathematical and logical computations on operands.

We know many operators from school. They are things like addition +, multiplication *, subtraction -, and so on.

in this article, we’ll start with the simple operators, then concentrate on the javaScript-specific aspects, that are not covered by school arithmetic

But before we move on, let’s grasp some common terminology.

  • An operand – is what operators are applied to. For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2. Sometimes, people call these “arguments” instead of “operands”.
  • An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:

let x = 1;

x = - x ;

alert( x )

Maths

The following math operations are supported:

  • Addition +,
  • Subtraction “,
  • Multiplication “,
  • Division /,
  • Remainder %,
  • Exponentiation *.

The first four are straightforward, while % and ** need a few words about them.

Despite its visual similarity, the modulus operator (%), is unrelated to percentages.

The result of a % b is the remainder of the integer division of a by b

For instance:

alert( 5 % 2 ); // 1, the remainder of 5 divided by 2

alert( 8 % 3 ); // 2, the remainder of 8 divided by 3

alert( 8 % 4 ); // 0, the remainder of 8 divided by 4

JavaScript Comparison Operators

JavaScript Comparison Operators are crucial for equality or difference comparisons between values. Here is a summary of some important comparison operators:

OPERATOR NAME OPERATION
Equality(==) Compares the equality of two operators.
Inequality(!=) Compares inequality of two operators.
Strict Equality(===) Compares both value and type of the operand.
Strict Inequality(!==) Compares inequality with type.
Greater than(>) Checks if the left operator is greater than the right operator.
Greater than or equal(>=) Checks if the left operator is greater than or equal to the right operator.
Less than(<) Checks if the left operator is smaller than the right operator.
Less than or equal(<=) Checks if the left operator is smaller than or equal to the right operator.

String concatenation with binary

Let’s meet the features of JavaScript operators that are beyond school arithmetics.

Usually, the plus operator + sums numbers.

But, if the binary + is applied to strings, it merges (concatenates) them:

let s = "my" + "string";

alert(s); // mystring

Note that if any of the operands is a string, then the other one is converted to a string too.

For example:

alert( '1' + 2 ); // "12"

alert( 2 + '1' ); // "21"

See, it doesn’t matter whether the first operand is a string or the second one.

Operator precedence

When dealing with expressions that have multiple operators, the order in which they're executed is determined by their precedence – essentially, their default priority.

For example, in the expression 1 + 2 * 2, multiplication takes precedence over addition. This means multiplication is done first.

However, if we want to change this default order, we can use parentheses. So, instead of relying on the default precedence, we can write (1 + 2) * 2 to make sure the addition is done first.

In JavaScript, each operator has a priority number. The one with the higher number goes first. If two operators have the same priority, they're executed from left to right.

Remember: parentheses always have the final say in determining the order of operations, regardless of the default precedence.

And just a quick tip: unary operators (like negative sign -) have a higher priority than their binary counterparts. But don't worry too much about that detail; it's more of an advanced concept.

Here’s an extract from the precedence table (you don’t need to remember this, but note that unary operators are higher than corresponding binary ones):

Assignment

Let’s note that an assignment = is also an operator. It is listed in the precedence table with the very low priority of 2.

That’s why, when we assign a variable, like x = 2 * 2 + 1, the calculations are done first and then the = is evaluated, storing the result in x.

Bitwise operators

Bitwise operators treat arguments as 32-bit integer numbers and work on the level of their binary representation.

These operators are not JavaScript-specific. They are supported in most programming languages.

The list of operators:

  • AND ( & )
  • OR ( | )
  • XOR ( ^ )
  • NOT ( ~ )
  • LEFT SHIFT ( << )
  • RIGHT SHIFT ( >> )
  • ZERO-FILL RIGHT SHIFT ( >>> )

These operators are used very rarely when we need to fiddle with numbers on the very lowest (bitwise) level. We won’t need these operators any time soon, as web development has little use for them, but in some special areas, such as cryptography, they are useful. You can read the Bitwise Operators chapter on MDN when a need arises.

Comma

The comma operator , is one of the rarest and most unusual operators. Sometimes, it’s used to write shorter code, so we need to know it in order to understand what’s going on.

The comma operator allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated but only the result of the last one is returned.

Among my favorites are the bitwise operators, even though they might not be everyday tools in web development. There's something intriguing about working at the binary level, especially in specialized areas like cryptography. It's a reminder that JavaScript, with its vast array of operators, has applications beyond the typical web development landscape.

I encourage you to not just stop here but to continue exploring and experimenting with these operators in your own code. Whether you find elegance in the simplicity of addition or the intricacies of bitwise manipulation, JavaScript has a rich tapestry waiting to be woven by developers like you.

So, as you sip your virtual coffee and code away, remember that each operator is like a brushstroke on the canvas of your program, contributing to the masterpiece you're creating.

buy me a coffee☕

javascriptprogrammingtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.