Comments are annotations in the source code, which are ignored by the interpreter. So, comments can be used to prevent the execution of the statements or text in the source code. Whenever we want to ignore somthing from the execution in the source code, we use comments. Comments can also be used to explain complex code logic, so the other developers can understand easily. There are two types of comments in javascript, such as-
Single line comments:-
// This is single line comment
document.getElementById("id").innerHTML = "Hello Tutorials Logic!";
Multi line comments:-
/*
This is multi line comment
*/
document.getElementById("id").innerHTML = "Hello Tutorials Logic!";
Variables are the containers in the memory which holds the data value and it can be changed anytime. In JavaScript variable can be declare by reserved keyword "var".
Standard Rules for JavaScript Variable Names:-
var x = 5; // Number
var Y = "Tutorials Logic!"; // String
var Z; // Declared without assigning a value
var x = 5, Y = "Tutorials Logic!", Z; // Multiple variable in a single Line
Modern JavaScript (ES6+) introduced let and const to address the scoping issues of var. Understanding the differences is essential for writing clean, bug-free code.
// var - function-scoped, hoisted, can be re-declared
var count = 1;
var count = 2; // no error
console.log(count); // 2
// let - block-scoped, not re-declarable in same scope
let score = 10;
score = 20; // reassignment OK
// let score = 30; // SyntaxError: already declared
// const - block-scoped, must be initialized, cannot be reassigned
const PI = 3.14159;
// PI = 3; // TypeError: Assignment to constant variable
// const with objects - the reference is constant, not the content
const user = { name: 'Alice', age: 25 };
user.age = 26; // OK - modifying property
// user = {}; // TypeError - cannot reassign
// Block scope demonstration
{
let blockVar = 'inside block';
const blockConst = 'also inside';
console.log(blockVar); // inside block
}
// console.log(blockVar); // ReferenceError
// var leaks out of blocks (but not functions)
{
var leaked = 'I leak!';
}
console.log(leaked); // I leak!
// camelCase for variables and functions (standard JS convention)
let firstName = 'John';
let totalPrice = 99.99;
function calculateTax() {}
// PascalCase for classes and constructors
class UserProfile {}
function Person(name) { this.name = name; }
// UPPER_SNAKE_CASE for constants
const MAX_RETRIES = 3;
const API_BASE_URL = 'https://api.example.com';
// Prefix booleans with is/has/can/should
let isActive = true;
let hasPermission = false;
let canEdit = true;
// Descriptive names - avoid single letters except in loops
for (let i = 0; i < 10; i++) { /* i is fine here */ }
// Bad vs Good naming
let d = new Date(); // bad
let currentDate = new Date(); // good
let arr = [1, 2, 3]; // bad
let userIds = [1, 2, 3]; // good
Explore 500+ free tutorials across 20+ languages and frameworks.