Basic rules of Javascript

Posted on Jan 28, 2023

JavaScript is a powerful programming language that can be used to create interactive and dynamic websites. It is a versatile language that can be used to add all sorts of functionality to a website, from simple form validation to complex animations and games. In this blog post, we will be discussing some of the basic rules of JavaScript that every beginner should know.

First and foremost, it is important to understand that JavaScript is a case-sensitive language. This means that variables and function names must be written in the same case as they were defined. For example, the variable “myVariable” is not the same as “myvariable”.

Next, JavaScript uses a strict syntax for declaring variables. Variables must always start with a letter, dollar sign, or underscore, and can only contain letters, numbers, and underscores. Additionally, variables must be declared using the var, let, or const keyword. For example:

var myVariable = "Hello World";
let secondVariable = 42;
const pi = 3.14;

JavaScript also uses a specific syntax for declaring and calling functions. Functions are defined using the “function” keyword, followed by the function name, a set of parentheses, and a set of curly braces. For example:

function myFunction() {
   console.log("Hello World");
}

To call a function, you simply use the function name followed by a set of parentheses, like so:

myFunction();

Finally, JavaScript uses a specific syntax for declaring and calling objects. Objects are defined using the “object” keyword, followed by the object name, a set of parentheses, and a set of curly braces. For example:

myFunction();

JavaScript also uses a specific syntax for controlling the flow of a program using conditional statements. Conditional statements are used to perform different actions depending on whether a certain condition is met. For example, you can use an if statement to check if a variable is equal to a certain value and perform an action if it is:

if (myVariable === "Hello World") {
   console.log("The variable is correct");
} else {
   console.log("The variable is not correct");
}

Lastly, JavaScript also uses a specific syntax for creating loops. Loops are used to repeatedly execute a block of code until a certain condition is met. For example, you can use a “for” loop to iterate through an array and perform an action for each element:

for (let i = 0; i < myArray.length; i++) {
   console.log(myArray[i]);
}

These are just a few of the basic rules of JavaScript that every beginner should know. By understanding and following these rules, you can create powerful and effective JavaScript programs.