Top 10 Must Know Javascript Rules You Should to Follow

Some Javascript Rules shown here

Here are some important JavaScript rules to follow when working:

  1. JavaScript is case sensitive, so be careful to use the correct capitalization when naming variables, functions, and keywords.
  2. Declare variables with the “var” keyword before using them.
  3. Use camelCase when naming variables.
  4. Use curly braces to denote the beginning and end of blocks of code.
  5. Use the “null” value to represent an empty or non-existent value.
  6. Use “undefined” to check if a variable has been declared or not.
  7. Use single quotes for strings, unless you need to use a string that contains single quotes itself, in which case you can use double quotes.
  8. Use the “console.log()” function to print messages to the console for debugging purposes.
  9. Use comments to describe your code and make it easier to understand.
  10. Make sure to end statements with a semicolon.

By following these rules, you can help ensure that your JavaScript code is clean, well-organized, and easy to understand.

What is Case Sensitive JavaScript?

In programming languages, “case sensitive” means that the language treats uppercase and lowercase letters as different characters. For example, in JavaScript, the variables “name” and “Name” are treated as two different variables, and the keyword “function” is not the same as “Function”.

It is important to be aware of this when working with JavaScript, as using the wrong case for a variable or keyword can cause syntax errors or lead to unexpected behavior in your code. It is generally a good idea to use a consistent naming convention, such as camelCase or snake_case, to help avoid these types of issues.

Let/Const/Var?

In JavaScript, it is generally a good idea to use the “const” keyword to declare variables that you do not plan to reassign, and the “let” keyword for variables that you do plan to reassign. The “var” keyword should generally be avoided, as it has some unusual behavior and is less widely used than “let” and “const”.

One reason to use “const” is that it can help you catch bugs in your code early on. If you try to reassign a value to a “const” variable, you will get a runtime error, which can alert you to the problem. With “var”, you may not realize that you are reassigning a value until much later, when it becomes more difficult to track down the issue.

Another reason to use “const” is that it can improve the readability of your code. By clearly indicating which variables are intended to be reassigned and which are not, you can make it easier for others (or for your future self) to understand your code.

Overall, it is generally a good idea to use “const” whenever possible, and only use “let” when you need to reassign a variable.

console.log() — Usage

The most proper way to use the “console.log()” function in JavaScript is to pass it a value or expression that you want to print to the console. For example:

Copy codeconsole.log("Hello, world!");
console.log(1 + 1);
console.log(myVariable);

You can also pass multiple values or expressions to “console.log()”, separated by commas. These values will be printed to the console separated by spaces. For example:

Copy codeconsole.log("Hello,", "world!");
console.log(1 + 1, 2 + 2, 3 + 3);
console.log(myVariable, myOtherVariable);

It is generally a good idea to use “console.log()” for debugging purposes, to help you understand how your code is running and to track down any issues. You should remove or comment out any “console.log()” statements that you are not using before deploying your code in production.

Overall, using “console.log()” in a clear and concise manner can help make your code more readable and easier to debug.

Simple Clean JavaScript Example

Here is some simple, clean JavaScript code that demonstrates some basic concepts:

Copy code// Declare a variable and assign it a value
const message = "Hello, world!";

// Define a function
function sayHello() {
  console.log(message);
}

// Call the function
sayHello();

This code declares a constant variable called “message” and assigns it the string value “Hello, world!”. It then defines a function called “sayHello()” that prints the value of “message” to the console. Finally, it calls the “sayHello()” function to execute it.

To make this code even cleaner, you could add comments to describe what each part of the code is doing, like this:

Copy code// Declare a constant variable and assign it a string value
const message = "Hello, world!";

// Define a function that prints a message to the console
function sayHello() {
  console.log(message);
}

// Call the function to execute it
sayHello();

By adding comments and using clear, descriptive names for variables and functions, you can make your code easier to read and understand.

What is Null/Underfined/Empty String?

In JavaScript, “null” is a special value that represents the absence of a value or a null reference. It is an assignment value that can be assigned to a variable as a representation of no value.

“Undefined” is a property of the global object, and it indicates that a variable has not been assigned a value. If you try to access a variable that has not been defined, it will be undefined.

An empty string is a string with zero length, meaning it does not contain any characters. It is represented by two quotation marks with nothing in between, like this: “”

Here are some examples of how these values can be used in JavaScript:

Copy codelet myVariable = null;  // The value of myVariable is null

if (typeof myOtherVariable === "undefined") {  // Check if myOtherVariable is undefined
  console.log("myOtherVariable is undefined");
}

let myString = "";  // The value of myString is an empty string

It is important to be aware of these special values in JavaScript, as they can be used in different ways and can sometimes behave differently than other values.