Skip to content

Commit 5b181a3

Browse files
authored
Spaces for the code blocks that are not converted in GitHub pages correctly
1 parent 223d8a8 commit 5b181a3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Scripts/javascript.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
- [Include blank line separator between imports and the rest of the code](#include-blank-line-separator-between-imports-and-the-rest-of-the-code)
3232
- [General best practices](../Generals/generals.md#general-best-practices)
3333

34+
3435
---
3536

3637

38+
3739
# JAVASCRIPT/TYPESCRIPT
3840

3941
## JAVASCRIPT
@@ -42,9 +44,12 @@
4244

4345
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.
4446

47+
48+
4549
### WHITESPACING AND FORMATTING
4650

4751
#### ALWAYS USE BRACES
52+
4853
```TypeScript
4954
// Don't do this
5055
if (blah === "foo")
@@ -56,7 +61,10 @@ if (blah === "foo") {
5661
}
5762
```
5863

64+
65+
5966
#### SAME LINE BRACES
67+
6068
```TypeScript
6169
// Don't do this
6270
if (blah === "foo")
@@ -72,10 +80,13 @@ if (blah === "foo") {
7280

7381
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.
7482

83+
84+
7585
#### CHARACTER SPACING
7686

7787
- Use a single space between values, variables, and operators.
7888
- Use a single space after commas.
89+
7990
```TypeScript
8091
// Don't do this
8192
var x=123;
@@ -94,7 +105,10 @@ if (blah === "foo") {
94105
}
95106
```
96107

108+
109+
97110
#### USE 4 SPACES INDENTATION
111+
98112
```TypeScript
99113
// Don't do this
100114
if (blah === "foo") {
@@ -107,7 +121,10 @@ if (blah === "foo") {
107121
}
108122
```
109123

124+
125+
110126
#### ALWAYS USE SEMICOLON
127+
111128
```TypeScript
112129
// Don't do this
113130
var foo = true
@@ -124,7 +141,10 @@ if (foo) {
124141
}
125142
```
126143

144+
145+
127146
#### ALWAYS USE COMPARISON
147+
128148
```TypeScript
129149
// Don't do this
130150
if (blah == "foo") {
@@ -139,9 +159,11 @@ if (blah === "foo") {
139159

140160
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.
141161

162+
142163
##### EXCEPTION
143164

144165
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+
145167
```TypeScript
146168
var foo = null;
147169

@@ -151,7 +173,10 @@ if (foo == null && bar == null) {
151173
}
152174
```
153175

176+
177+
154178
#### AVOID COMPARING TO TRUE AND FALSE
179+
155180
```TypeScript
156181
// Don't do this as it's redundant
157182
if (foo === true) {
@@ -172,13 +197,18 @@ if (!bar) {
172197
}
173198
```
174199

200+
201+
175202
### VARIABLES
176203

177204
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 `$`.
178205

206+
207+
179208
#### CAMEL CASE VARIABLES
180209

181210
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+
182212
```TypeScript
183213
// Don't do this
184214
let MyVar = "blah";
@@ -187,9 +217,12 @@ let MyVar = "blah";
187217
let myVar = "blah";
188218
```
189219

220+
221+
190222
#### BOOLEAN VARIABLES
191223

192224
Booleans should be easily identifiable by the way they are named. Use prefixes like `is``can` or `has` to propose a question.
225+
193226
```TypeScript
194227
// Do this
195228
let isEditing = true;
@@ -199,7 +232,10 @@ obj.canEdit = true;
199232
user.hasPermission = true;
200233
```
201234

235+
236+
202237
### USE TERNARY IF STATEMENT WHENEVER POSSIBLE AND EASY TO READ
238+
203239
```TypeScript
204240
// Don't do this
205241
if (foo) {
@@ -212,11 +248,16 @@ if (foo) {
212248
foo ? blah("foo") : blah("bar");
213249
```
214250

251+
252+
215253
### AVOID POLLUTING THE GLOBAL NAMESPACE
216254

217255
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.
218256

257+
258+
219259
### CHECK EXISTANCE OF VARIABLES, ARRAYS AND OBJECTS
260+
220261
```TypeScript
221262
if (element) {
222263
// element exists
@@ -237,11 +278,14 @@ Please note that the same `if` statement is always returning `true` for all of
237278
- empty arrays `[]` and empty objects `{}`: because they are defined;
238279
- spaced string `' '`: because it is a valid and defined object.
239280

281+
282+
240283
## TYPESCRIPT SPECIFIC STANDARDS
241284

242285
### WHITESPACING AND FORMATTING
243286

244287
#### INCLUDE A SINGLE SPACE AFTER COLON AND BEFORE AND AFTER EQUAL
288+
245289
```TypeScript
246290
// Don't do this
247291
let str : string = 'This is a definition of a string';
@@ -254,7 +298,10 @@ let bool: boolean = false;
254298
let nr: number = 123;
255299
```
256300

301+
302+
257303
#### ALWAYS DEFINE STRICT TYPE WHEN DECLARING VARIABLES
304+
258305
```TypeScript
259306
// Don't do this
260307
let str = 'This is a definition of a string';
@@ -267,7 +314,10 @@ let bool: boolean = false;
267314
let nr: number = 123;
268315
```
269316

317+
318+
270319
#### INCLUDE A SINGLE SPACE BEFORE AND AFTER CURLY BRAKETS WHEN IMPORTING
320+
271321
```TypeScript
272322
// Don't do this
273323
import {ComponentName} from './path/to/component';
@@ -276,7 +326,10 @@ import {ComponentName} from './path/to/component';
276326
import { ComponentName } from './path/to/component';
277327
```
278328

329+
330+
279331
#### USE SINGLE QUOTES IN TYPESCRIPT FILES
332+
280333
```TypeScript
281334
// Don't do this
282335
import { ComponentName } from "./path/to/component";
@@ -289,7 +342,10 @@ import { ComponentName } from './path/to/component';
289342
let str: string = 'This is a definition of a string';
290343
```
291344

345+
346+
292347
#### INCLUDE BLANK LINE SEPARATOR BETWEEN IMPORTS AND THE REST OF THE CODE
348+
293349
```TypeScript
294350
// Don't do this
295351
import { ComponentName } from './path/to/component';

0 commit comments

Comments
 (0)