JavaScript: Unary Operators

This is what I learned about unary operators while working on a 8kyu Codewars challenge in further detail.

What are Unary Operators in JavaScript?

To better understand what unary operators are I took apart the word Uni which equals One whil…


This content originally appeared on DEV Community and was authored by thatgirlvictoria

This is what I learned about unary operators while working on a 8kyu Codewars challenge in further detail.

What are Unary Operators in JavaScript?

To better understand what unary operators are I took apart the word Uni which equals One while operators are a character that represents an action.

Unary operators work on one value. Below is a table to better explain:

table of unary operators

What does this table mean exactly?

  • Non-numeric values: all unary operators will first convert them into a number.

  • Unary plus (+) when placed before a numeric value, it does nothing.

  • Unary minus (-) when placed before a numeric value, it will negate it.

  • Prefix increment operator (++ before value) adds one to a value. Value is changed before the statement is evaluated.

  • Postfix decrement operator (-- before value) minus one to a value. Value is changed before the statement is evaluated.

  • Postfix increment operator (++ before value) adds one to a value. Value is changed after the statement is evaluated.

  • Prefix decrement operator (-- before value) minus one to a value. Value is changed after the statement is evaluated.

Codewars Challenge: Opposite Number

How did I use unary operators to solve a problem?

The goal: If you are given an integer or a floating-point number, find its opposite.

Examples:
   1: -1
   14: -14
   -34: 34

I love writing code that is easy to read, the cleanest way I thought to answer the problem was to use unary minus operator.

function opposite(number) {
  return(-number);
}


This content originally appeared on DEV Community and was authored by thatgirlvictoria


Print Share Comment Cite Upload Translate Updates
APA

thatgirlvictoria | Sciencx (2022-06-01T21:57:20+00:00) JavaScript: Unary Operators. Retrieved from https://www.scien.cx/2022/06/01/javascript-unary-operators/

MLA
" » JavaScript: Unary Operators." thatgirlvictoria | Sciencx - Wednesday June 1, 2022, https://www.scien.cx/2022/06/01/javascript-unary-operators/
HARVARD
thatgirlvictoria | Sciencx Wednesday June 1, 2022 » JavaScript: Unary Operators., viewed ,<https://www.scien.cx/2022/06/01/javascript-unary-operators/>
VANCOUVER
thatgirlvictoria | Sciencx - » JavaScript: Unary Operators. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/01/javascript-unary-operators/
CHICAGO
" » JavaScript: Unary Operators." thatgirlvictoria | Sciencx - Accessed . https://www.scien.cx/2022/06/01/javascript-unary-operators/
IEEE
" » JavaScript: Unary Operators." thatgirlvictoria | Sciencx [Online]. Available: https://www.scien.cx/2022/06/01/javascript-unary-operators/. [Accessed: ]
rf:citation
» JavaScript: Unary Operators | thatgirlvictoria | Sciencx | https://www.scien.cx/2022/06/01/javascript-unary-operators/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.