You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Scripts/javascript.md
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,9 +31,11 @@
31
31
- [Include blank line separator between imports and the rest of the code](#include-blank-line-separator-between-imports-and-the-rest-of-the-code)
32
32
-[General best practices](../Generals/generals.md#general-best-practices)
33
33
34
+
34
35
---
35
36
36
37
38
+
37
39
# JAVASCRIPT/TYPESCRIPT
38
40
39
41
## JAVASCRIPT
@@ -42,9 +44,12 @@
42
44
43
45
When loading a script, the browser cannot continue until the entire file has been loaded. If we have JavaScript files in order to add functionality, we should place those files at the bottom, just before the closing body tag. This is a good performance practice and the results are quite noticeable.
44
46
47
+
48
+
45
49
### WHITESPACING AND FORMATTING
46
50
47
51
#### ALWAYS USE BRACES
52
+
48
53
```TypeScript
49
54
// Don't do this
50
55
if (blah==="foo")
@@ -56,7 +61,10 @@ if (blah === "foo") {
56
61
}
57
62
```
58
63
64
+
65
+
59
66
#### SAME LINE BRACES
67
+
60
68
```TypeScript
61
69
// Don't do this
62
70
if (blah==="foo")
@@ -72,10 +80,13 @@ if (blah === "foo") {
72
80
73
81
Main reason for this is simply that is a [JavaScript pitfall](https://stackoverflow.com/questions/3641519/why-do-results-vary-based-on-curly-brace-placement), but also it looks nicer.
74
82
83
+
84
+
75
85
#### CHARACTER SPACING
76
86
77
87
- Use a single space between values, variables, and operators.
78
88
- Use a single space after commas.
89
+
79
90
```TypeScript
80
91
// Don't do this
81
92
var x=123;
@@ -94,7 +105,10 @@ if (blah === "foo") {
94
105
}
95
106
```
96
107
108
+
109
+
97
110
#### USE 4 SPACES INDENTATION
111
+
98
112
```TypeScript
99
113
// Don't do this
100
114
if (blah==="foo") {
@@ -107,7 +121,10 @@ if (blah === "foo") {
107
121
}
108
122
```
109
123
124
+
125
+
110
126
#### ALWAYS USE SEMICOLON
127
+
111
128
```TypeScript
112
129
// Don't do this
113
130
var foo =true
@@ -124,7 +141,10 @@ if (foo) {
124
141
}
125
142
```
126
143
144
+
145
+
127
146
#### ALWAYS USE COMPARISON
147
+
128
148
```TypeScript
129
149
// Don't do this
130
150
if (blah=="foo") {
@@ -139,9 +159,11 @@ if (blah === "foo") {
139
159
140
160
The use of the `==` equality operator allows frustrating bugs to slip through almost undetected while the use of the strict equality operator `===` does not run type coercion and therefore strictly evaluates the difference between two objects.
141
161
162
+
142
163
##### EXCEPTION
143
164
144
165
Double equals comparison is allowed when comparing to `null`, because it will detect both `null` or `undefined` properties. [Here a great article about this exception](https://medium.com/javascript-in-plain-english/how-to-check-for-null-in-javascript-dffab64d8ed5#:~:text=One%20way%20to%20check,the%20double%20equality%20%3D%3D%20operator%3A&text=So%2C%20when%20programming%20to%20check,for%20either%20null%20or%20undefined%20.) and [here the MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
166
+
145
167
```TypeScript
146
168
var foo =null;
147
169
@@ -151,7 +173,10 @@ if (foo == null && bar == null) {
151
173
}
152
174
```
153
175
176
+
177
+
154
178
#### AVOID COMPARING TO TRUE AND FALSE
179
+
155
180
```TypeScript
156
181
// Don't do this as it's redundant
157
182
if (foo===true) {
@@ -172,13 +197,18 @@ if (!bar) {
172
197
}
173
198
```
174
199
200
+
201
+
175
202
### VARIABLES
176
203
177
204
Use meaningful names when creating a variable: think about how the variable is going to serve your purpose. If the variable is caching a jQuery CSS selector, give it the name of the selector prefixed with `$`.
178
205
206
+
207
+
179
208
#### CAMEL CASE VARIABLES
180
209
181
210
The camel casing (or *camelCasing*) of JavaScript variables is accepted as the standard in most coding environments. The only exception is the use of uppercase to denote constants and underscores to denotate private variables (TypeScript).
211
+
182
212
```TypeScript
183
213
// Don't do this
184
214
let MyVar ="blah";
@@ -187,9 +217,12 @@ let MyVar = "blah";
187
217
let myVar ="blah";
188
218
```
189
219
220
+
221
+
190
222
#### BOOLEAN VARIABLES
191
223
192
224
Booleans should be easily identifiable by the way they are named. Use prefixes like `is`, `can` or `has` to propose a question.
225
+
193
226
```TypeScript
194
227
// Do this
195
228
let isEditing =true;
@@ -199,7 +232,10 @@ obj.canEdit = true;
199
232
user.hasPermission=true;
200
233
```
201
234
235
+
236
+
202
237
### USE TERNARY IF STATEMENT WHENEVER POSSIBLE AND EASY TO READ
238
+
203
239
```TypeScript
204
240
// Don't do this
205
241
if (foo) {
@@ -212,11 +248,16 @@ if (foo) {
212
248
foo?blah("foo") :blah("bar");
213
249
```
214
250
251
+
252
+
215
253
### AVOID POLLUTING THE GLOBAL NAMESPACE
216
254
217
255
The chance of script and variable conflicts is increased, and both the source file and the namespace itself become littered with countless ambiguously named variables.
218
256
257
+
258
+
219
259
### CHECK EXISTANCE OF VARIABLES, ARRAYS AND OBJECTS
260
+
220
261
```TypeScript
221
262
if (element) {
222
263
// element exists
@@ -237,11 +278,14 @@ Please note that the same `if` statement is always returning `true` for all of
237
278
- empty arrays `[]` and empty objects `{}`: because they are defined;
238
279
- spaced string `' '`: because it is a valid and defined object.
239
280
281
+
282
+
240
283
## TYPESCRIPT SPECIFIC STANDARDS
241
284
242
285
### WHITESPACING AND FORMATTING
243
286
244
287
#### INCLUDE A SINGLE SPACE AFTER COLON AND BEFORE AND AFTER EQUAL
288
+
245
289
```TypeScript
246
290
// Don't do this
247
291
let str :string='This is a definition of a string';
@@ -254,7 +298,10 @@ let bool: boolean = false;
254
298
let nr:number=123;
255
299
```
256
300
301
+
302
+
257
303
#### ALWAYS DEFINE STRICT TYPE WHEN DECLARING VARIABLES
304
+
258
305
```TypeScript
259
306
// Don't do this
260
307
let str ='This is a definition of a string';
@@ -267,7 +314,10 @@ let bool: boolean = false;
267
314
let nr:number=123;
268
315
```
269
316
317
+
318
+
270
319
#### INCLUDE A SINGLE SPACE BEFORE AND AFTER CURLY BRAKETS WHEN IMPORTING
320
+
271
321
```TypeScript
272
322
// Don't do this
273
323
import {ComponentName} from'./path/to/component';
@@ -276,7 +326,10 @@ import {ComponentName} from './path/to/component';
0 commit comments