Day 1: Defining Variables in JavaScript

I’ve started Hitesh Sir’s JavaScript 30 days challenge, and today’s task was all about defining variables and understanding their types. If you haven’t joined yet, you can check it out here. Hurry, it’s free until July 14th!

Declaring Variables

In JavaScript, you can declare variables using three keywords: let, var, and const. Here’s a quick overview:

let: Allows you to declare a variable that can be changed later.
var: Similar to let, but has some differences in scope handling.
const: Used to declare variables that should not be changed.
Example:

let myVariable = 'Hello';
var myOldVariable = 'World';
const myConstant = 42;
Enter fullscreen mode
Exit fullscreen mode

When you use let or var, you can change the value of the variable. However, if you declare a variable with const, attempting to change its value will result in an error.

Variable Types

You can assign various types of values to variables:
Number: For numerical values.
String: For text values.
Boolean: For true/false values.
undefined: For variables that are declared but not yet assigned a value.
null: For variables explicitly set to have no value.
These are called primitive values.

Syntax
The syntax for declaring a variable is straightforward:

let/const/var variableName = value;

Think of a variable as a box, and the value as the content inside the box.

Key Points

typeof: This is an operator, not a function. It’s used to check the type of a variable.

console.log(typeof myVariable); // Output: string
Enter fullscreen mode
Exit fullscreen mode

console.table: Handy for displaying arrays and objects in a tabular format.

const fruits = ['Apple', 'Banana', 'Cherry'];
console.table(fruits); const user = { name: 'John', age: 25, city: 'New York'
};
console.table(user); 
Enter fullscreen mode
Exit fullscreen mode

Research Findings on Variable Declarations

I found an excellent resource on variable declarations at javascriptInfo. Here’s a summary of what I learned:

  • Difference between var and let: They are almost the same, but their scope handling is different. let is block-scoped while var is function-scoped.

  • Naming constants: Use uppercase letters for constant variable names if their values are already known. Otherwise, use camelCase.

  • Redeclaration: You cannot declare the same variable again using let or var, but you can change its value multiple times (except for const variables).

  • Naming conventions: Variable names should preferably be in camelCase (though it’s not strictly required).

  • Declaring multiple variables: It’s better to declare multiple variables on separate lines for readability.

let user = 'John';
let age = 25;
let message = 'Hello';
Enter fullscreen mode
Exit fullscreen mode

Instead of:

let user = 'John', age = 25, message = 'Hello'; 
Enter fullscreen mode
Exit fullscreen mode
  • Meaningful names: Always give meaningful names to your variables.
    For more detailed information, you can follow thisblog article.

Feel free to tweak this further as per your style. Happy coding!


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

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

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Free & easy ad network.