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 Varia…


This content originally appeared on DEV Community and was authored by Tejas Khanolkar

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;

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

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);

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';

Instead of:

let user = 'John', age = 25, message = 'Hello';

  • 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!


This content originally appeared on DEV Community and was authored by Tejas Khanolkar


Print Share Comment Cite Upload Translate Updates
APA

Tejas Khanolkar | Sciencx (2024-07-13T22:01:51+00:00) Day 1: Defining Variables in JavaScript. Retrieved from https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/

MLA
" » Day 1: Defining Variables in JavaScript." Tejas Khanolkar | Sciencx - Saturday July 13, 2024, https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/
HARVARD
Tejas Khanolkar | Sciencx Saturday July 13, 2024 » Day 1: Defining Variables in JavaScript., viewed ,<https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/>
VANCOUVER
Tejas Khanolkar | Sciencx - » Day 1: Defining Variables in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/
CHICAGO
" » Day 1: Defining Variables in JavaScript." Tejas Khanolkar | Sciencx - Accessed . https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/
IEEE
" » Day 1: Defining Variables in JavaScript." Tejas Khanolkar | Sciencx [Online]. Available: https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/. [Accessed: ]
rf:citation
» Day 1: Defining Variables in JavaScript | Tejas Khanolkar | Sciencx | https://www.scien.cx/2024/07/13/day-1-defining-variables-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.