forked from aame1/class-9-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunary.js
35 lines (27 loc) · 1.28 KB
/
unary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
*
+ as an Addition Operator & + as a Unary Version in Javascript Objects
+ is an arithmetic operator (take numerical values (either literals or variables) as their operands and return a single numerical value)
+ as a unary operator (an operation with only one operand.)
The + and - operators also have unary versions, where they operate only on one variable. When used in this fashion, + returns the number representation of the object, while - returns its negative counterpart
The unary plus operator converts its operand to Number type.
The unary negation operator converts its operand to Number type and then negates it.
Run this code here: https://jsfiddle.net/6wu1kxr8/1/
*
*/
let codes = {
"+1": "plus",
"-1": "minus",
// "1": "no-plus-nothing", //Duplicate key
1: "no-strings-attached"
};
// Evaluates to string type
for (let code in codes) console.log(codes[code], code, typeof code);
// Evaluates to number type
for (let code in codes) console.log(codes[code], +code, typeof +code);
// Negates the sign and turn it to number
for (let code in codes) console.log(codes[code], -code, typeof -code);
/**
* https://javascript.info/object#the-for-in-loop
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#Unary_plus_(.2B)
*/