-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathunary-operators.html
More file actions
227 lines (191 loc) · 63.3 KB
/
unary-operators.html
File metadata and controls
227 lines (191 loc) · 63.3 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>JavsScript - Unary Operators</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="The typeof operator, Overview, The delete operator, The unary plus operator (+), The void operator, The unary negation operator (-), The bitwise NOT operator (~), The logical NOT operator (!)">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="JavsScript - Unary Operators">
<meta property="og:description" content="The typeof operator, Overview, The delete operator, The unary plus operator (+), The void operator, The unary negation operator (-), The bitwise NOT operator (~), The logical NOT operator (!)">
<meta property="og:type" content="article">
<meta property="og:url" content="/javascript/unary-operators.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="JavsScript - Unary Operators">
<meta name="twitter:description" content="The typeof operator, Overview, The delete operator, The unary plus operator (+), The void operator, The unary negation operator (-), The bitwise NOT operator (~), The logical NOT operator (!)">
<meta name="twitter:url" content="/javascript/unary-operators.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/1795.cc0fbc5a.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/javascript/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/javascript/getting-started-with-javascript.html" class="sidebar-link">Getting started with JavaScript</a></li><li><a href="/javascript/javascript-variables.html" class="sidebar-link">JavaScript Variables</a></li><li><a href="/javascript/built-in-constants.html" class="sidebar-link">Built-in Constants</a></li><li><a href="/javascript/comments.html" class="sidebar-link">Comments</a></li><li><a href="/javascript/console.html" class="sidebar-link">Console</a></li><li><a href="/javascript/datatypes-in-javascript.html" class="sidebar-link">Datatypes in Javascript</a></li><li><a href="/javascript/strings.html" class="sidebar-link">Strings</a></li><li><a href="/javascript/date.html" class="sidebar-link">Date</a></li><li><a href="/javascript/date-comparison.html" class="sidebar-link">Date Comparison</a></li><li><a href="/javascript/comparison-operations.html" class="sidebar-link">Comparison Operations</a></li><li><a href="/javascript/conditions.html" class="sidebar-link">Conditions</a></li><li><a href="/javascript/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/javascript/objects.html" class="sidebar-link">Objects</a></li><li><a href="/javascript/arithmetic-math.html" class="sidebar-link">Arithmetic (Math)</a></li><li><a href="/javascript/bitwise-operators.html" class="sidebar-link">Bitwise operators</a></li><li><a href="/javascript/constructor-functions.html" class="sidebar-link">Constructor functions</a></li><li><a href="/javascript/declarations-and-assignments.html" class="sidebar-link">Declarations and Assignments</a></li><li><a href="/javascript/loops.html" class="sidebar-link">Loops</a></li><li><a href="/javascript/functions.html" class="sidebar-link">Functions</a></li><li><a href="/javascript/functional-javascript.html" class="sidebar-link">Functional JavaScript</a></li><li><a href="/javascript/prototypes-objects.html" class="sidebar-link">Prototypes, objects</a></li><li><a href="/javascript/classes.html" class="sidebar-link">Classes</a></li><li><a href="/javascript/namespacing.html" class="sidebar-link">Namespacing</a></li><li><a href="/javascript/context-this.html" class="sidebar-link">Context (this)</a></li><li><a href="/javascript/setters-and-getters.html" class="sidebar-link">Setters and Getters</a></li><li><a href="/javascript/events.html" class="sidebar-link">Events</a></li><li><a href="/javascript/inheritance.html" class="sidebar-link">Inheritance</a></li><li><a href="/javascript/method-chaining.html" class="sidebar-link">Method Chaining</a></li><li><a href="/javascript/callbacks.html" class="sidebar-link">Callbacks</a></li><li><a href="/javascript/intervals-and-timeouts.html" class="sidebar-link">Intervals and Timeouts</a></li><li><a href="/javascript/regular-expressions.html" class="sidebar-link">Regular expressions</a></li><li><a href="/javascript/cookies.html" class="sidebar-link">Cookies</a></li><li><a href="/javascript/web-storage.html" class="sidebar-link">Web Storage</a></li><li><a href="/javascript/data-attributes.html" class="sidebar-link">Data attributes</a></li><li><a href="/javascript/json.html" class="sidebar-link">JSON</a></li><li><a href="/javascript/ajax.html" class="sidebar-link">AJAX</a></li><li><a href="/javascript/enumerations.html" class="sidebar-link">Enumerations</a></li><li><a href="/javascript/map.html" class="sidebar-link">Map</a></li><li><a href="/javascript/timestamps.html" class="sidebar-link">Timestamps</a></li><li><a href="/javascript/unary-operators.html" aria-current="page" class="active sidebar-link">Unary Operators</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-typeof-operator" class="sidebar-link">The typeof operator</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#overview" class="sidebar-link">Overview</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-delete-operator" class="sidebar-link">The delete operator</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-unary-plus-operator" class="sidebar-link">The unary plus operator (+)</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-void-operator" class="sidebar-link">The void operator</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-unary-negation-operator" class="sidebar-link">The unary negation operator (-)</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-bitwise-not-operator" class="sidebar-link">The bitwise NOT operator (~)</a></li><li class="sidebar-sub-header"><a href="/javascript/unary-operators.html#the-logical-not-operator" class="sidebar-link">The logical NOT operator (!)</a></li></ul></li><li><a href="/javascript/generators.html" class="sidebar-link">Generators</a></li><li><a href="/javascript/promises.html" class="sidebar-link">Promises</a></li><li><a href="/javascript/set.html" class="sidebar-link">Set</a></li><li><a href="/javascript/modals-prompts.html" class="sidebar-link">Modals - Prompts</a></li><li><a href="/javascript/execcommand-and-contenteditable.html" class="sidebar-link">execCommand and contenteditable</a></li><li><a href="/javascript/history.html" class="sidebar-link">History</a></li><li><a href="/javascript/navigator-object.html" class="sidebar-link">Navigator Object</a></li><li><a href="/javascript/bom-browser-object-model.html" class="sidebar-link">BOM (Browser Object Model)</a></li><li><a href="/javascript/the-event-loop.html" class="sidebar-link">The Event Loop</a></li><li><a href="/javascript/strict-mode.html" class="sidebar-link">Strict mode</a></li><li><a href="/javascript/custom-elements.html" class="sidebar-link">Custom Elements</a></li><li><a href="/javascript/data-manipulation.html" class="sidebar-link">Data Manipulation</a></li><li><a href="/javascript/binary-data.html" class="sidebar-link">Binary Data</a></li><li><a href="/javascript/template-literals.html" class="sidebar-link">Template Literals</a></li><li><a href="/javascript/fetch.html" class="sidebar-link">Fetch</a></li><li><a href="/javascript/scope.html" class="sidebar-link">Scope</a></li><li><a href="/javascript/modules.html" class="sidebar-link">Modules</a></li><li><a href="/javascript/screen.html" class="sidebar-link">Screen</a></li><li><a href="/javascript/variable-coercion-conversion.html" class="sidebar-link">Variable coercion/conversion</a></li><li><a href="/javascript/destructuring-assignment.html" class="sidebar-link">Destructuring assignment</a></li><li><a href="/javascript/websockets.html" class="sidebar-link">WebSockets</a></li><li><a href="/javascript/arrow-functions.html" class="sidebar-link">Arrow Functions</a></li><li><a href="/javascript/workers.html" class="sidebar-link">Workers</a></li><li><a href="/javascript/requestanimationframe.html" class="sidebar-link">requestAnimationFrame</a></li><li><a href="/javascript/creational-design-patterns.html" class="sidebar-link">Creational Design Patterns</a></li><li><a href="/javascript/detecting-browser.html" class="sidebar-link">Detecting browser</a></li><li><a href="/javascript/symbols.html" class="sidebar-link">Symbols</a></li><li><a href="/javascript/transpiling.html" class="sidebar-link">Transpiling</a></li><li><a href="/javascript/automatic-semicolon-insertion-asi.html" class="sidebar-link">Automatic Semicolon Insertion - ASI</a></li><li><a href="/javascript/localization.html" class="sidebar-link">Localization</a></li><li><a href="/javascript/geolocation.html" class="sidebar-link">Geolocation</a></li><li><a href="/javascript/indexeddb.html" class="sidebar-link">IndexedDB</a></li><li><a href="/javascript/modularization-techniques.html" class="sidebar-link">Modularization Techniques</a></li><li><a href="/javascript/proxy.html" class="sidebar-link">Proxy</a></li><li><a href="/javascript/postmessage-and-messageevent.html" class="sidebar-link">.postMessage() and MessageEvent</a></li><li><a href="/javascript/weakmap.html" class="sidebar-link">WeakMap</a></li><li><a href="/javascript/weakset.html" class="sidebar-link">WeakSet</a></li><li><a href="/javascript/escape-sequences.html" class="sidebar-link">Escape Sequences</a></li><li><a href="/javascript/behavioral-design-patterns.html" class="sidebar-link">Behavioral Design Patterns</a></li><li><a href="/javascript/server-sent-events.html" class="sidebar-link">Server-sent events</a></li><li><a href="/javascript/async-functions-async-await.html" class="sidebar-link">Async functions (async/await)</a></li><li><a href="/javascript/async-iterators.html" class="sidebar-link">Async Iterators</a></li><li><a href="/javascript/how-to-make-iterator-usable-inside-async-callback-function.html" class="sidebar-link">How to make iterator usable inside async callback function</a></li><li><a href="/javascript/tail-call-optimization.html" class="sidebar-link">Tail Call Optimization</a></li><li><a href="/javascript/bitwise-operators-real-world-examples-snippets.html" class="sidebar-link">Bitwise Operators - Real World Examples (snippets)</a></li><li><a href="/javascript/tilde.html" class="sidebar-link">Tilde ~</a></li><li><a href="/javascript/using-javascript-to-get-set-css-custom-variables.html" class="sidebar-link">Using javascript to get/set CSS custom variables</a></li><li><a href="/javascript/selection-api.html" class="sidebar-link">Selection API</a></li><li><a href="/javascript/file-api-blobs-and-filereaders.html" class="sidebar-link">File API, Blobs and FileReaders</a></li><li><a href="/javascript/notifications-api.html" class="sidebar-link">Notifications API</a></li><li><a href="/javascript/vibration-api.html" class="sidebar-link">Vibration API</a></li><li><a href="/javascript/battery-status-api.html" class="sidebar-link">Battery Status API</a></li><li><a href="/javascript/fluent-api.html" class="sidebar-link">Fluent API</a></li><li><a href="/javascript/web-cryptography-api.html" class="sidebar-link">Web Cryptography API</a></li><li><a href="/javascript/security-issues.html" class="sidebar-link">Security issues</a></li><li><a href="/javascript/same-origin-policy-cross-origin-communication.html" class="sidebar-link">Same Origin Policy & Cross-Origin Communication</a></li><li><a href="/javascript/error-handling.html" class="sidebar-link">Error Handling</a></li><li><a href="/javascript/global-error-handling-in-browsers.html" class="sidebar-link">Global error handling in browsers</a></li><li><a href="/javascript/debugging.html" class="sidebar-link">Debugging</a></li><li><a href="/javascript/unit-testing-javascript.html" class="sidebar-link">Unit Testing Javascript</a></li><li><a href="/javascript/evaluating-javascript.html" class="sidebar-link">Evaluating JavaScript</a></li><li><a href="/javascript/linters-ensuring-code-quality.html" class="sidebar-link">Linters - Ensuring code quality</a></li><li><a href="/javascript/anti-patterns.html" class="sidebar-link">Anti-patterns</a></li><li><a href="/javascript/performance-tips.html" class="sidebar-link">Performance Tips</a></li><li><a href="/javascript/memory-efficiency.html" class="sidebar-link">Memory efficiency</a></li><li><a href="/javascript/reserved-keywords.html" class="sidebar-link">Reserved Keywords</a></li><li><a href="/javascript/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="unary-operators"><a href="#unary-operators" class="header-anchor">#</a> Unary Operators</h1> <h2 id="the-typeof-operator"><a href="#the-typeof-operator" class="header-anchor">#</a> The typeof operator</h2> <p>The <code>typeof</code> operator returns the data type of the unevaluated operand as a string.</p> <h3 id="syntax"><a href="#syntax" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">typeof</span> operand
</code></pre></div><h3 id="returns"><a href="#returns" class="header-anchor">#</a> Returns:</h3> <p>These are the possible return values from <code>typeof</code>:</p> <table><thead><tr><th>Type</th> <th>Return value</th></tr></thead> <tbody><tr><td><code>Undefined</code></td> <td><code>"undefined"</code></td></tr> <tr><td><code>Null</code></td> <td><code>"object"</code></td></tr> <tr><td><code>Boolean</code></td> <td><code>"boolean"</code></td></tr> <tr><td><code>Number</code></td> <td><code>"number"</code></td></tr> <tr><td><code>String</code></td> <td><code>"string"</code></td></tr> <tr><td><code>Symbol</code> (ES6)</td> <td><code>"symbol"</code></td></tr> <tr><td><code>Function</code> object</td> <td><code>"function"</code></td></tr> <tr><td><code>document.all</code></td> <td><code>"undefined"</code></td></tr> <tr><td>Host object (provided by the JS environment)</td> <td>Implementation-dependent</td></tr> <tr><td>Any other object</td> <td><code>"object"</code></td></tr></tbody></table> <p>The unusual behavior of <code>document.all</code> with the <code>typeof</code> operator is from its former usage to detect legacy browsers. For more information, see <a href="http://stackoverflow.com/q/40643613/6388243" target="_blank" rel="noopener noreferrer">Why is document.all defined but typeof document.all returns "undefined"?<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="examples"><a href="#examples" class="header-anchor">#</a> Examples:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// returns 'number'</span>
<span class="token keyword">typeof</span> <span class="token number">3.14</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token number">Infinity</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token number">NaN</span><span class="token punctuation">;</span> <span class="token comment">// "Not-a-Number" is a "number"</span>
<span class="token comment">// returns 'string'</span>
<span class="token keyword">typeof</span> <span class="token string">""</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token string">"bla"</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// typeof always returns a string</span>
<span class="token comment">// returns 'boolean'</span>
<span class="token keyword">typeof</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token comment">// returns 'undefined'</span>
<span class="token keyword">typeof</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> declaredButUndefinedVariable<span class="token punctuation">;</span>
<span class="token keyword">typeof</span> undeclaredVariable<span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token keyword">void</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> document<span class="token punctuation">.</span>all <span class="token comment">// see above</span>
<span class="token comment">// returns 'function'</span>
<span class="token keyword">typeof</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token keyword">class</span> <span class="token class-name">C</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> Math<span class="token punctuation">.</span>sin<span class="token punctuation">;</span>
<span class="token comment">// returns 'object'</span>
<span class="token keyword">typeof</span> <span class="token punctuation">{</span> <span class="token comment">/*<...>*/</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token operator">/</span>regex<span class="token operator">/</span><span class="token punctuation">;</span> <span class="token comment">// This is also considered an object</span>
<span class="token keyword">typeof</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// use Array.isArray or Object.prototype.toString.call.</span>
<span class="token keyword">typeof</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> <span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Don't use!</span>
<span class="token keyword">typeof</span> <span class="token keyword">new</span> <span class="token class-name">Number</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Don't use!</span>
<span class="token keyword">typeof</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"abc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Don't use!</span>
<span class="token comment">// returns 'symbol'</span>
<span class="token keyword">typeof</span> <span class="token function">Symbol</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">typeof</span> Symbol<span class="token punctuation">.</span>iterator<span class="token punctuation">;</span>
</code></pre></div><h2 id="overview"><a href="#overview" class="header-anchor">#</a> Overview</h2> <p>Unary operators are operators with only one operand. Unary operators are more efficient than standard JavaScript function calls. Additionally, unary operators can not be overridden and therefore their functionality is guaranteed.</p> <p>The following unary operators are available:</p> <table><thead><tr><th>Operator</th> <th>Operation</th> <th>Example</th></tr></thead> <tbody><tr><td><code>delete</code></td> <td>The delete operator deletes a property from an object.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6837/the-delete-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>void</code></td> <td>The void operator discards an expression's return value.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6839/the-void-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>typeof</code></td> <td>The typeof operator determines the type of a given object.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6838/the-typeof-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>+</code></td> <td>The unary plus operator converts its operand to Number type.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6836/the-unary-plus-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>-</code></td> <td>The unary negation operator converts its operand to Number, then negates it.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6840/the-unary-negation-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>~</code></td> <td>Bitwise NOT operator.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6841/the-bitwise-not-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr> <tr><td><code>!</code></td> <td>Logical NOT operator.</td> <td><a href="http://stackoverflow.com/documentation/javascript/2084/unary-operators/6842/the-logical-not-operator" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></td></tr></tbody></table> <h2 id="the-delete-operator"><a href="#the-delete-operator" class="header-anchor">#</a> The delete operator</h2> <p>The <code>delete</code> operator deletes a property from an object.</p> <h3 id="syntax-2"><a href="#syntax-2" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">delete</span> object<span class="token punctuation">.</span>property
<span class="token keyword">delete</span> object<span class="token punctuation">[</span><span class="token string">'property'</span><span class="token punctuation">]</span>
</code></pre></div><h3 id="returns-2"><a href="#returns-2" class="header-anchor">#</a> Returns:</h3> <p>If deletion is successful, or the property did not exist:</p> <ul><li><code>true</code></li></ul> <p>If the property to be deleted is an own non-configurable property (can't be deleted):</p> <ul><li><code>false</code> in non-strict mode.</li> <li>Throws an error in strict mode</li></ul> <h3 id="description"><a href="#description" class="header-anchor">#</a> Description</h3> <p>The <code>delete</code> operator does not directly free memory. It can indirectly free memory if the operation means all references to the property are gone.</p> <p><code>delete</code> works on an object's properties. If a property with the same name exists on the object's prototype chain, the property will be inherited from the prototype.<br> <code>delete</code> does not work on variables or function names.</p> <h3 id="examples-2"><a href="#examples-2" class="header-anchor">#</a> Examples:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Deleting a property</span>
foo <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// a global variable is a property of `window`: `window.foo`</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">;</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Uncaught ReferenceError: foo is not defined</span>
<span class="token comment">// Deleting a variable</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">;</span> <span class="token comment">// false</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1 (Not deleted)</span>
<span class="token comment">// Deleting a function</span>
<span class="token keyword">function</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">;</span> <span class="token comment">// false</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// function foo(){ } (Not deleted)</span>
<span class="token comment">// Deleting a property</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span> bar<span class="token operator">:</span> <span class="token string">"42"</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">.</span>bar<span class="token punctuation">;</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Object { } (Deleted bar)</span>
<span class="token comment">// Deleting a property that does not exist</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">delete</span> foo<span class="token punctuation">.</span>bar<span class="token punctuation">;</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Object { } (No errors, nothing deleted)</span>
<span class="token comment">// Deleting a non-configurable property of a predefined object</span>
<span class="token keyword">delete</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">;</span> <span class="token comment">// false ()</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 3.141592653589793 (Not deleted)</span>
</code></pre></div><h2 id="the-unary-plus-operator"><a href="#the-unary-plus-operator" class="header-anchor">#</a> The unary plus operator (+)</h2> <p>The unary plus (<code>+</code>) precedes its operand <strong>and evaluates</strong> to its operand. It attempts to convert the operand to a number, if it isn't already.</p> <h3 id="syntax-3"><a href="#syntax-3" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">+</span>expression
</code></pre></div><h3 id="returns-3"><a href="#returns-3" class="header-anchor">#</a> Returns:</h3> <ul><li>a <code>Number</code>.</li></ul> <h3 id="description-2"><a href="#description-2" class="header-anchor">#</a> Description</h3> <p>The unary plus (<code>+</code>) operator is the fastest (and preferred) method of converting something into a number.</p> <p>It can convert:</p> <ul><li>string representations of integers (decimal or hexadecimal) and floats.</li> <li>booleans: <code>true</code>, <code>false</code>.</li> <li><code>null</code></li></ul> <p>Values that can't be converted will evaluate to <code>NaN</code>.</p> <h3 id="examples-3"><a href="#examples-3" class="header-anchor">#</a> Examples:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">+</span><span class="token number">42</span> <span class="token comment">// 42</span>
<span class="token operator">+</span><span class="token string">"42"</span> <span class="token comment">// 42</span>
<span class="token operator">+</span><span class="token boolean">true</span> <span class="token comment">// 1</span>
<span class="token operator">+</span><span class="token boolean">false</span> <span class="token comment">// 0</span>
<span class="token operator">+</span><span class="token keyword">null</span> <span class="token comment">// 0</span>
<span class="token operator">+</span><span class="token keyword">undefined</span> <span class="token comment">// NaN</span>
<span class="token operator">+</span><span class="token number">NaN</span> <span class="token comment">// NaN</span>
<span class="token operator">+</span><span class="token string">"foo"</span> <span class="token comment">// NaN</span>
<span class="token operator">+</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// NaN</span>
<span class="token operator">+</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// NaN</span>
</code></pre></div><p>Note that attempting to convert an array can result in unexpected return values.<br>
In the background, arrays are first converted to their string representations:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">''</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'1'</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'1,2'</span><span class="token punctuation">;</span>
</code></pre></div><p>The operator then attempts to convert those strings to numbers:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">+</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment">// 0 ( === +'' )</span>
<span class="token operator">+</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// 1 ( === +'1' )</span>
<span class="token operator">+</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">// NaN ( === +'1,2' )</span>
</code></pre></div><h2 id="the-void-operator"><a href="#the-void-operator" class="header-anchor">#</a> The void operator</h2> <p>The <code>void</code> operator evaluates the given expression and then returns <code>undefined</code>.</p> <h3 id="syntax-4"><a href="#syntax-4" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">void</span> expression
</code></pre></div><h3 id="returns-4"><a href="#returns-4" class="header-anchor">#</a> Returns:</h3> <ul><li><code>undefined</code></li></ul> <h3 id="description-3"><a href="#description-3" class="header-anchor">#</a> Description</h3> <p>The <code>void</code> operator is often used to obtain the <code>undefined</code> primitive value, by means of writing <code>void 0</code> or <code>void(0)</code>. Note that <code>void</code> is an operator, not a function, so <code>()</code> is not required.</p> <p>Usually the result of a <code>void</code> expression and <code>undefined</code> can be used interchangeably.<br>
However, in older versions of ECMAScript, <code>window.undefined</code> could be assigned any value, and it is still possible to use <code>undefined</code> as name for function parameters variables inside functions, thus disrupting other code that relies on the value of <code>undefined</code>.<br> <code>void</code> will always yield the <strong>true</strong> <code>undefined</code> value though.</p> <p><code>void 0</code> is also commonly used in code minification as a shorter way of writing <code>undefined</code>. In addition, it's probably safer as some other code could've tampered with <code>window.undefined</code>.</p> <h3 id="examples-4"><a href="#examples-4" class="header-anchor">#</a> Examples:</h3> <p>Returning <code>undefined</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">void</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// undefined</span>
</code></pre></div><p>Changing the value of <code>undefined</code> inside a certain scope:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword">undefined</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str <span class="token operator">===</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="the-unary-negation-operator"><a href="#the-unary-negation-operator" class="header-anchor">#</a> The unary negation operator (-)</h2> <p>The unary negation (<code>-</code>) precedes its operand and negates it, after trying to convert it to number.</p> <h3 id="syntax-5"><a href="#syntax-5" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">-</span>expression
</code></pre></div><h3 id="returns-5"><a href="#returns-5" class="header-anchor">#</a> Returns:</h3> <ul><li>a <code>Number</code>.</li></ul> <h3 id="description-4"><a href="#description-4" class="header-anchor">#</a> Description</h3> <p>The unary negation (<code>-</code>) can convert the same types / values as the unary plus (<code>+</code>) operator can.</p> <p>Values that can't be converted will evaluate to <code>NaN</code> (there is no <code>-NaN</code>).</p> <h3 id="examples-5"><a href="#examples-5" class="header-anchor">#</a> Examples:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">-</span><span class="token number">42</span> <span class="token comment">// -42</span>
<span class="token operator">-</span><span class="token string">"42"</span> <span class="token comment">// -42</span>
<span class="token operator">-</span><span class="token boolean">true</span> <span class="token comment">// -1</span>
<span class="token operator">-</span><span class="token boolean">false</span> <span class="token comment">// -0</span>
<span class="token operator">-</span><span class="token keyword">null</span> <span class="token comment">// -0</span>
<span class="token operator">-</span><span class="token keyword">undefined</span> <span class="token comment">// NaN</span>
<span class="token operator">-</span><span class="token number">NaN</span> <span class="token comment">// NaN</span>
<span class="token operator">-</span><span class="token string">"foo"</span> <span class="token comment">// NaN</span>
<span class="token operator">-</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// NaN</span>
<span class="token operator">-</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// NaN</span>
</code></pre></div><p>Note that attempting to convert an array can result in unexpected return values.<br>
In the background, arrays are first converted to their string representations:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">''</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'1'</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'1,2'</span><span class="token punctuation">;</span>
</code></pre></div><p>The operator then attempts to convert those strings to numbers:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">-</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment">// -0 ( === -'' )</span>
<span class="token operator">-</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// -1 ( === -'1' )</span>
<span class="token operator">-</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">// NaN ( === -'1,2' )</span>
</code></pre></div><h2 id="the-bitwise-not-operator"><a href="#the-bitwise-not-operator" class="header-anchor">#</a> The bitwise NOT operator (~)</h2> <p>The bitwise NOT (<code>~</code>) performs a NOT operation on each bit in a value.</p> <h3 id="syntax-6"><a href="#syntax-6" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">~</span>expression
</code></pre></div><h3 id="returns-6"><a href="#returns-6" class="header-anchor">#</a> Returns:</h3> <ul><li>a <code>Number</code>.</li></ul> <h3 id="description-5"><a href="#description-5" class="header-anchor">#</a> Description</h3> <p>The truth table for the NOT operation is:</p> <table><thead><tr><th>a</th> <th>NOT a</th></tr></thead> <tbody><tr><td>0</td> <td>1</td></tr> <tr><td>1</td> <td>0</td></tr></tbody></table> <div class="language-js extra-class"><pre class="language-js"><code><span class="token number">1337</span> <span class="token punctuation">(</span>base <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">0000010100111001</span> <span class="token punctuation">(</span>base <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token operator">~</span><span class="token number">1337</span> <span class="token punctuation">(</span>base <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">1111101011000110</span> <span class="token punctuation">(</span>base <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1338</span> <span class="token punctuation">(</span>base <span class="token number">10</span><span class="token punctuation">)</span>
</code></pre></div><p>A bitwise not on a number results in: <code>-(x + 1)</code>.</p> <h3 id="examples-6"><a href="#examples-6" class="header-anchor">#</a> Examples:</h3> <table><thead><tr><th>value (base 10)</th> <th>value (base 2)</th> <th>return (base 2)</th> <th>return (base 10)</th></tr></thead> <tbody><tr><td><code>2</code></td> <td><code>00000010</code></td> <td><code>11111100</code></td> <td><code>-3</code></td></tr> <tr><td><code>1</code></td> <td><code>00000001</code></td> <td><code>11111110</code></td> <td><code>-2</code></td></tr> <tr><td><code>0</code></td> <td><code>00000000</code></td> <td><code>11111111</code></td> <td><code>-1</code></td></tr> <tr><td><code>-1</code></td> <td><code>11111111</code></td> <td><code>00000000</code></td> <td><code>0</code></td></tr> <tr><td><code>-2</code></td> <td><code>11111110</code></td> <td><code>00000001</code></td> <td><code>1</code></td></tr> <tr><td><code>-3</code></td> <td><code>11111100</code></td> <td><code>00000010</code></td> <td><code>2</code></td></tr></tbody></table> <h2 id="the-logical-not-operator"><a href="#the-logical-not-operator" class="header-anchor">#</a> The logical NOT operator (!)</h2> <p>The logical NOT (<code>!</code>) operator performs logical negation on an expression.</p> <h3 id="syntax-7"><a href="#syntax-7" class="header-anchor">#</a> Syntax:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">!</span>expression
</code></pre></div><h3 id="returns-7"><a href="#returns-7" class="header-anchor">#</a> Returns:</h3> <ul><li>a <code>Boolean</code>.</li></ul> <h3 id="description-6"><a href="#description-6" class="header-anchor">#</a> Description</h3> <p>The logical NOT (<code>!</code>) operator performs logical negation on an expression.</p> <p>Boolean values simply get inverted: <code>!true === false</code> and <code>!false === true</code>.<br>
Non-boolean values get converted to boolean values first, then are negated.</p> <p>This means that a double logical NOT (<code>!!</code>) can be used to cast any value to a boolean:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">!</span><span class="token operator">!</span><span class="token string">"FooBar"</span> <span class="token operator">===</span> <span class="token boolean">true</span>
<span class="token operator">!</span><span class="token operator">!</span><span class="token number">1</span> <span class="token operator">===</span> <span class="token boolean">true</span>
<span class="token operator">!</span><span class="token operator">!</span><span class="token number">0</span> <span class="token operator">===</span> <span class="token boolean">false</span>
</code></pre></div><p>These are all equal to <code>!true</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">!</span><span class="token string">'true'</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token string">'false'</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token string">'false'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token string">'FooBar'</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>These are all equal to <code>!false</code>:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">!</span><span class="token number">0</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token string">''</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token number">NaN</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token keyword">null</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">!</span><span class="token keyword">undefined</span> <span class="token operator">===</span> <span class="token operator">!</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="examples-7"><a href="#examples-7" class="header-anchor">#</a> Examples:</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator">!</span><span class="token boolean">true</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token operator">-</span><span class="token number">1</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token string">"-1"</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token number">42</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token string">"42"</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token string">"foo"</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token string">"true"</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token string">"false"</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// false</span>
<span class="token operator">!</span><span class="token boolean">false</span> <span class="token comment">// true</span>
<span class="token operator">!</span><span class="token keyword">null</span> <span class="token comment">// true</span>
<span class="token operator">!</span><span class="token keyword">undefined</span> <span class="token comment">// true</span>
<span class="token operator">!</span><span class="token number">NaN</span> <span class="token comment">// true</span>
<span class="token operator">!</span><span class="token number">0</span> <span class="token comment">// true</span>
<span class="token operator">!</span><span class="token string">""</span> <span class="token comment">// true</span>
</code></pre></div><h4 id="syntax-8"><a href="#syntax-8" class="header-anchor">#</a> Syntax</h4> <ul><li>void expression; // Evaluates expression and discards return value</li> <li>+expression; // Attempt to convert expression to a number</li> <li>delete object.property; // Delete object's property</li> <li>delete object["property"]; // Delete object's property</li> <li>typeof operand; // Returns type of operand</li> <li>~expression; // Perform NOT operation on each bit of expression</li> <li>!expression; // Perform logical negation on expression</li> <li>-expression; // Negate expression after attempting conversion to number</li></ul></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/javascript/unary-operators.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/javascript/timestamps.html" class="prev">
Timestamps
</a></span> <span class="next"><a href="/javascript/generators.html">
Generators
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/1795.cc0fbc5a.js" defer></script>
</body>
</html>