Variables and Data Types

Variables and Data Types

In this section, we will explore variables and data types in JavaScript. Understanding these concepts is essential for managing and manipulating data in your programs.

Variables

Variables are containers for storing data values. In JavaScript, you can declare a variable using var, let, or const.

Declaring Variables

  1. var: The var keyword declares a variable. It has function scope or global scope.

    var name = 'codeswithpankaj';
  2. let: The let keyword declares a block-scoped variable, meaning it is only available within the block it is defined.

    let age = 25;
  3. const: The const keyword declares a block-scoped, read-only variable. The value of a const variable cannot be changed once it is assigned.

    const pi = 3.14;

Example:

var userName = 'codeswithpankaj';
let userAge = 30;
const userGender = 'Male';

console.log(userName); // Outputs: codeswithpankaj
console.log(userAge); // Outputs: 30
console.log(userGender); // Outputs: Male

Data Types

JavaScript provides various data types to work with different kinds of values. Here are the most commonly used data types:

  1. String: Represents textual data.

    let greeting = 'Hello, codeswithpankaj';
  2. Number: Represents both integer and floating-point numbers.

    let score = 100;
    let price = 29.99;
  3. Boolean: Represents logical values: true or false.

    let isLoggedIn = true;
  4. Array: Represents an ordered list of values.

    let colors = ['red', 'green', 'blue'];
  5. Object: Represents a collection of key-value pairs.

    let person = {
      name: 'Pankaj',
      age: 25,
      gender: 'Male'
    };
  6. Undefined: Represents a variable that has been declared but not yet assigned a value.

    let x;
    console.log(x); // Outputs: undefined
  7. Null: Represents the intentional absence of any object value.

    let y = null;
  8. Symbol: Represents a unique and immutable primitive value, often used as the key of an object property.

    let sym = Symbol('unique');

Examples of Data Types

String

let siteName = 'codeswithpankaj';
console.log(siteName); // Outputs: codeswithpankaj

Number

let visitorCount = 1500;
let avgRating = 4.7;
console.log(visitorCount); // Outputs: 1500
console.log(avgRating); // Outputs: 4.7

Boolean

let isSubscribed = true;
console.log(isSubscribed); // Outputs: true

Array

let tutorialTopics = ['JavaScript', 'CSS', 'HTML'];
console.log(tutorialTopics); // Outputs: ['JavaScript', 'CSS', 'HTML']

Object

let tutorialDetails = {
  title: 'Introduction to JavaScript',
  author: 'codeswithpankaj',
  length: 50
};
console.log(tutorialDetails);
// Outputs: { title: 'Introduction to JavaScript', author: 'codeswithpankaj', length: 50 }

Undefined

let notAssigned;
console.log(notAssigned); // Outputs: undefined

Null

let noValue = null;
console.log(noValue); // Outputs: null

Symbol

let uniqueID = Symbol('id');
console.log(uniqueID); // Outputs: Symbol(id)

Summary

Variables and data types are fundamental concepts in JavaScript. By understanding how to declare and use variables, and recognizing the different data types, you can manage and manipulate data effectively in your programs. Practice these concepts to build a strong foundation in JavaScript.


Last updated