Menu Close

What are the 3 basic rules for JavaScript?

JavaScript is a powerful programming language commonly used for creating interactive websites. Understanding the three basic rules of JavaScript is crucial for mastering the language and building robust applications. The first rule is to always end statements with a semicolon to separate different code lines and prevent errors.

Secondly, JavaScript is case-sensitive, meaning that variables, functions, and keywords must be written with consistent capitalization throughout the code. Lastly, proper indentation and formatting are essential for code readability and organization, making it easier for developers to troubleshoot and maintain their projects. Mastering these three basic rules sets a strong foundation for writing efficient and error-free JavaScript code.

JavaScript is a versatile programming language that adds interactivity and dynamic content to websites. Whether you’re a beginner or an experienced coder, it’s essential to understand the basic rules of JavaScript to write clean and efficient code. In this article, we will explore the three fundamental rules that will set a strong foundation for your JavaScript journey.

Rule 1 – Variable Declaration and Naming

One of the first things you’ll encounter when learning JavaScript is declaring variables. Variables act as containers for storing data that can change over time. To declare a variable, use the ‘var’, ‘let’, or ‘const’ keyword followed by a unique name.

It’s crucial to follow proper naming conventions for variables. Here are a few rules to keep in mind:

  1. Start with a letter or underscore: Variable names should begin with a letter (A-Z) or an underscore (_).
  2. Avoid reserved keywords: You cannot use keywords like ‘if’, ‘function,’ or ‘while’ as variable names since they serve specific purposes in JavaScript.
  3. Camel case or underscores: Choose between camel case (myVariable) or underscores (my_variable) for better readability.

Rule 2 – Comments and Readability

Comments are essential for code maintainability and collaboration. They make your code more readable by explaining its purpose and functionality. JavaScript supports two types of comments:

  1. Single-line comments: Use double slashes (//) to add comments for a single line.
  2. Multi-line comments: Enclose multiple lines of comments between ‘/*’ and ‘*/.’

When writing code, proper indentation and consistent formatting are crucial for readability. Use spaces, line breaks, and appropriate indentation to ensure your code is easy to understand both for yourself and others who may work on it.

Rule 3 – Understanding Scope

Global Scope

The concept of scope determines the accessibility of variables, functions, and objects in your code. JavaScript has two primary scopes:

Global Scope refers to variables declared outside of any function or block, making them accessible throughout the entire code. It’s important to be cautious while using global variables as they can be accessed and modified by any part of your code, which may lead to unexpected behavior.


var globalVariable = ‘I am a global variable’;

function printGlobal() {

printGlobal(); // Output: ‘I am a global variable’

Local Scope

Local Scope refers to variables declared within a function or block. These variables are only accessible inside the function or block in which they are declared.


function myFunction() {
var localVariable = ‘I am a local variable’;

myFunction(); // Output: ‘I am a local variable’
console.log(localVariable); // Output: Uncaught ReferenceError: localVariable is not defined

Understanding scope is crucial for writing maintainable and bug-free code. It helps prevent variable name clashes, promotes encapsulation, and improves code readability.

By following these three fundamental rules for JavaScript, you’ll be well on your way to becoming a proficient JavaScript developer. Remember to declare variables correctly, write readable code utilizing comments and proper formatting, and understand the concept of scope. Embracing these principles will lead to cleaner code, fewer errors, and ultimately create a solid foundation for your JavaScript projects.

Mastering the 3 basic rules for JavaScript – understanding variable declarations, using proper syntax, and paying attention to case sensitivity – is essential for writing efficient and error-free code. By following these rules, programmers can enhance their coding skills and produce high-quality JavaScript applications.

Leave a Reply

Your email address will not be published. Required fields are marked *