-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path2_variable_naming_conventions.js
64 lines (47 loc) · 2.49 KB
/
2_variable_naming_conventions.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
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
// In JavaScript, naming conventions for variables are not strict, but it's important to follow certain best practices to make your code readable and maintainable. Here are some common conventions for naming variables in JavaScript:
// Use descriptive names: Choose names that clearly describe the purpose of the variable.This makes your code more readable and easier to understand.
// Use camelCase: When naming variables, use camelCase, which means the first word is lowercase and subsequent words are capitalized.This convention is widely used in JavaScript.
// Avoid using reserved words: JavaScript has reserved words that cannot be used as variable names, such as "if", "else", and "while".Make sure to avoid using these words as variable names to prevent errors.
// Use meaningful abbreviations: If a variable name is too long, you can use meaningful abbreviations to make it shorter while still maintaining its clarity.
// Use consistent naming: Use consistent naming conventions throughout your code to make it easier to read and understand.For example, if you use camelCase for variable names, use it consistently across all your variables.
// Here's an example of how to apply these conventions to variable names:
// Good variable names:
var firstName = "John";
var lastName = "Doe";
var numItems = 10;
var userLoggedIn = true;
// Avoid using reserved words:
// var if = 5; // This will throw an error
// Use consistent naming conventions:
var userName = "johndoe";
var userEmail = "[email protected]";
var userAddress = "123 Main St.";
// Following these naming conventions for variables in JavaScript will make your code more readable and easier to maintain, both for yourself and for others who may work with your code in the future.
/*
Ref :
https://www.w3schools.com/js/js_reserved.asp
*/
// 1. Which of the following variable names is NOT a valid variable name in JavaScript?
// A. myVar
// B. $price
// C. 123abc
// D. camelCase
// Answer: C. 123abc
// 2. Which of the following is a valid way to declare a variable in JavaScript?
// A. variable name = value;
// B. var name = value;
// C. name = value;
// D. let name = value;
// Answer: D. let name = value;
// 3. Which of the following is a recommended naming convention for variables in JavaScript?
// A. snake_case
// B. camelCase
// C. PascalCase
// D. kebab-case
// Answer: B. camelCase
// 4. Which of the following variable names is more descriptive and follows the naming convention?
// A. myCar
// B. car
// C. Mycar
// D. my_car
// Answer: A. myCar