-
Notifications
You must be signed in to change notification settings - Fork 37
/
JavaScript-vs-PowerShell.htm
291 lines (256 loc) · 4.75 KB
/
JavaScript-vs-PowerShell.htm
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, minimum-scale=0.25">
<title>JavaScript vs. PowerShell vs. Python</title>
<style>
@media (prefers-color-scheme: dark) {
*, a {
background-color: #121212;
color: #ddd;
}
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/powershell.min.js"></script>
<script>
// see https://highlightjs.org/
hljs.initHighlightingOnLoad();
</script>
</head>
<body>
<table>
<tr>
<th>JavaScript
<th>PowerShell
<th>Python
<tr>
<td>
<pre><code class="javascript">
// This is a single line comment.
/* This is a
comment block.
JavaScript is case-sensitive.
*/
</code></pre>
</td>
<td>
<pre><code class="powershell">
# This is a single line comment.
<# This is a
comment block.
PowerShell is not case-sensitive.
#>
</code></pre>
</td>
<td>
<pre><code class=python>
# This is a single line comment.
"""This is a
docstring, not a comment.
Python is case-sensitive.
"""
</code></pre>
</td>
<tr>
<td>
<pre><code class="javascript">
// JavaScript variables can start with a letter, $, _
var v = 1;
const c = 2;
let b = 3;
stringDouble = "double quotes";
stringSingle = 'single quotes';
template = `a = ${a}`;
escaped = "\"";
</code></pre>
</td>
<td>
<pre><code class="powershell">
# PowerShell variables start with $
$v = 1
Set-Variable c -option constant -value 2
# n/a [use script:, global:, etc ?]
$stringDouble = "double quotes"
$stringSingle = 'single quotes' # no string interpolation
$template1 = "a = $a" # $a is simple.
$template2 = "a.b = $($a.b)" # $a.b is not simple.
$escaped = "`""
</code></pre>
</td>
<td>
<pre><code class=python>
# Python variables can start with a letter, _
v = 1
c = 2
b = 3
string_double = "double quotes"
string_single = 'single quotes'
f_string = f'a = {a}'
</code></pre>
</td>
<tr>
<td>
<pre><code class="javascript">
// an array
arr = [1, 2];
arr = [];
arr.push(3);
b = arr[0];
// no support for negative indexes
arr.slice(0, 3); // end is not included
arr.length;
</code></pre>
</td>
<td>
<pre><code class="powershell">
# an array
$arr = 1, 2
$arr = @()
$arr += 3
$b = $arr[0]
# negative indexes
$arr[0..2] # end is included
$arr.length # or $arr.count
</code></pre>
</td>
<td>
<pre><code class=python>
# a list
lst = [1, 2]
lst = []
lst.append(3)
b = lst[0]
lst[0:3] # end is not included
len(lst)
</code></pre>
</td>
<tr>
<td>
<pre><code class="javascript">
// an object
obj = {name1: "value", n2: 2};
obj = {};
obj.n3 = 3;
longObj = {
name1: "value",
n2: 2
};
user = {firstName: 'Gabriel', lastName: 'Sroka'};
</code></pre>
</td>
<td>
<pre><code class="powershell">
# a hashtable
$hash = @{name1 = "value"; n2 = 2}
$hash = @{}
$hash.n3 = 3
$longhash = @{ # longhash doesn't use semicolons.
name1 = "value"
n2 = 2
}
$user = @{firstName = 'Gabriel'; lastName = 'Sroka'}
</code></pre>
</td>
<td>
<pre><code class=python>
# a dictionary
dct = {'name1': 'value', 'n2': 2}
dct = {}
dct['n3'] = 3
long_dict = {
'name1': 'value',
'n2': 2
}
user = {'first_name': 'Gabriel', 'last_name': 'Sroka'}
</code></pre>
</td>
<tr>
<td>
<pre><code class="javascript">
// function name is usually a verb.
function add(a1, a2) {
return a1 + a2;
}
v = add(a1, a2);
</code></pre>
</td>
<td>
<pre><code class="powershell">
# function name is Verb-Noun. Must be declared before it's called.
function Add-Numbers($a1, $a2) {
$a1 + $a2 # return is optional.
}
$v = Add-Numbers $a1 $a2
</code></pre>
</td>
<td>
<pre><code class=python>
# function must be declared before it's called.
def add(a1, a2):
return a1 + a2
v = add(a1, a2)
</code></pre>
</td>
<tr>
<td>
<pre><code class="javascript">
if (a == b) /* ... */ // Braces are optional but recommended.
if (a < b) // ...
b = true;
if (a == b) {
//
} else if (b < c) {
//
} else {
//
}
while (b) { }
do { } while (b); // note the trailing semicolon
for (var i = 0; i < MAX; i++) { }
for (const item of items) {
console.log(item);
}
</code></pre>
</td>
<td>
<pre><code class="powershell">
if ($a -eq $b) { <# ... #> } # Braces are required.
if ($a -lt $b) { <# ... #> }
$b = $true
if ($a -eq $b) {
#
} elseif ($b -lt $c) {
#
} else {
#
}
while ($b) { }
do { } while ($b)
for ($i = 0; $i -lt $MAX; $i++) { }
foreach ($item in $items) {
Write-Host $item
}
</code></pre>
</td>
<td>
<pre><code class=python>
if a == b: # ...
if a < b: # ...
b = True
if a == b:
#
elif b < c:
#
else:
#
while b: #
# no do/while loop, use while/break
for i in range(MAX): # ...
for item in items:
print(item)
</code></pre>
</td>
</table>
</body>
</html>