JAVASCRIPT

DAY-3

30 Days Of JavaScript | DAY-3
30 Days Of JavaScript | DAY-3

JavaScript Day 3: Variables and Data Types Complete Tutorial

Welcome to Day 3 of the 30 Days of JavaScript challenge. Today we will dive deep into JavaScript variables and data types ÔÇö the building blocks of every JavaScript program. By the end of this tutorial you will know how to declare variables correctly, choose between var, let, and const, and work with every data type JavaScript offers.

Declaring Variables in JavaScript

JavaScript provides three keywords to declare variables: var, let, and const. Each behaves differently regarding scope and reassignment.

var age;
let name;
const PI = 3.14;
  • var ÔÇö function-scoped, can be redeclared and reassigned. Avoid in modern code.
  • let ÔÇö block-scoped, can be reassigned but not redeclared in the same scope.
  • const ÔÇö block-scoped, cannot be reassigned. Best for fixed values.

Assigning Values to Variables

The assignment operator = is used to give a value to a variable. You can assign during declaration or later.

age = 25;
name = "UpdateGadh";
let city = "Delhi"; // declared and assigned together

JavaScript Data Types Explained

JavaScript has 8 built-in data types divided into two categories: primitive and non-primitive (reference) types.

Primitive Data Types

  • Number ÔÇö Integers and floating-point numbers. Example: let age = 25;
  • String ÔÇö Text wrapped in quotes. Example: let name = "John Doe";
  • Boolean ÔÇö true or false. Example: let isActive = true;
  • Undefined ÔÇö A variable declared but not assigned. Example: let x; // undefined
  • Null ÔÇö Intentional absence of value. Example: let y = null;
  • Symbol ÔÇö A unique identifier introduced in ES6.
  • BigInt ÔÇö For integers larger than 2^53 – 1.

Non-Primitive Data Type

  • Object ÔÇö Collection of key-value pairs, includes arrays, functions, dates, etc.
let person = {
  name: "John Doe",
  age: 25,
  isActive: true
};

let numbers = [1, 2, 3, 4, 5]; // Array is also an Object

Dynamic Typing in JavaScript

JavaScript is a dynamically typed language ÔÇö meaning a variable can hold any type and its type can change at runtime.

let age = 25;          // Number
age = "Twenty-Five";   // Now String ÔÇö perfectly valid

Checking Variable Type with typeof

Use the typeof operator to check the data type of any variable.

let name = "John Doe";
console.log(typeof name); // Output: "string"

let age = 25;
console.log(typeof age);  // Output: "number"

let isActive = true;
console.log(typeof isActive); // Output: "boolean"

Type Conversion

JavaScript can convert between data types either implicitly or explicitly. Examples:

// Explicit conversion
Number("42");      // 42
String(42);        // "42"
Boolean(1);        // true

// Implicit conversion
"5" + 1;           // "51" (number is coerced to string)
"5" - 1;           // 4   (string is coerced to number)

Best Practices for Variables in JavaScript

  • Use const by default, switch to let only when reassignment is needed.
  • Avoid using var in modern JavaScript code.
  • Use meaningful variable names like userName instead of x.
  • Follow camelCase naming: firstName, not first_name.
  • Never use reserved keywords like class, function, return as variable names.

Conclusion

Understanding variables and data types is the foundation of JavaScript programming. Practice declaring variables, switching between data types, and using typeof to inspect them. Tomorrow on Day 4, we will explore operators and expressions in JavaScript. Keep coding and keep building!

Read More:

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

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

Chat with us