Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials


Comments and Variables

Comments

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:-

example
// This is single line comment
document.getElementById("id").innerHTML = "Hello Tutorials Logic!";

Multi line comments:-

example
/*
    This is multi line comment
*/
document.getElementById("id").innerHTML = "Hello Tutorials Logic!";

Variables

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:-

  • The variable name in JavaScript must begin with an english alphabet or the underscore character i.e.(A-Z, a-z, and underscore).
  • After first letter of variable name numeric character is allowed in the variable name i.e. (0 to 9).
  • A JavaScript variable name should not contain blank spaces.
  • JavaScript variables are case sensitive.
  • A variable name should not contain blank spaces.
example
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

var vs let vs const

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 vs let vs const
// 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!

Variable Naming Best Practices

Naming Conventions
// 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
Key Takeaways
  • Use const by default, let when you need to reassign, and avoid var in modern JavaScript.
  • var is function-scoped and hoisted; let and const are block-scoped and in the Temporal Dead Zone before declaration.
  • const prevents reassignment of the variable binding, but object/array contents can still be mutated.
  • Use camelCase for variables and functions, PascalCase for classes, UPPER_SNAKE_CASE for constants.
  • Prefix boolean variables with is, has, can, or should for clarity.
  • Comments should explain WHY, not WHAT - the code itself should be readable enough to explain what it does.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.