logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Mastering JavaScript Fundamentals

author
Generated by
Abhishek Goyan

15/10/2024

javascript

Sign in to read full article

Introduction to Variables

Variables are the backbone of any programming language, and JavaScript is no exception. They allow us to store and manipulate data in our code. Let's explore how to declare and use variables in JavaScript.

Declaring Variables

In JavaScript, we have three ways to declare variables:

  1. var (older method, function-scoped)
  2. let (block-scoped, recommended for variables that can be reassigned)
  3. const (block-scoped, for variables that won't be reassigned)
var oldSchool = "I'm function-scoped"; let modern = "I'm block-scoped"; const constant = "I can't be reassigned";

Pro tip: Always use let or const instead of var for better code organization and to avoid potential issues with variable scoping.

Understanding Data Types

JavaScript has several built-in data types that you'll work with frequently. Let's break them down:

Primitive Data Types

  1. Number: Represents both integer and floating-point numbers.

    let age = 25; let pi = 3.14159;
  2. String: Represents textual data.

    let name = "Alice"; let greeting = 'Hello, World!';
  3. Boolean: Represents true or false values.

    let isLoggedIn = true; let hasPermission = false;
  4. Undefined: Represents a variable that has been declared but not assigned a value.

    let undefinedVar; console.log(undefinedVar); // Output: undefined
  5. Null: Represents a deliberate non-value or absence of any object value.

    let emptyValue = null;
  6. Symbol: Represents a unique identifier (introduced in ES6).

    const uniqueId = Symbol('id');

Complex Data Types

  1. Object: Represents a collection of related data and/or functionality.

    let person = { name: "Bob", age: 30, isStudent: false };
  2. Array: A special type of object used to store multiple values in a single variable.

    let fruits = ["apple", "banana", "orange"];

Basic Operators

Operators allow us to perform operations on variables and values. Let's explore some essential operators in JavaScript:

Arithmetic Operators

These operators perform mathematical operations:

let a = 10; let b = 5; console.log(a + b); // Addition: 15 console.log(a - b); // Subtraction: 5 console.log(a * b); // Multiplication: 50 console.log(a / b); // Division: 2 console.log(a % b); // Modulus (remainder): 0 console.log(a ** b); // Exponentiation: 100000

Assignment Operators

These operators assign values to variables:

let x = 5; x += 3; // Equivalent to: x = x + 3 console.log(x); // Output: 8 x -= 2; // Equivalent to: x = x - 2 console.log(x); // Output: 6 x *= 4; // Equivalent to: x = x * 4 console.log(x); // Output: 24 x /= 3; // Equivalent to: x = x / 3 console.log(x); // Output: 8

Comparison Operators

These operators compare two values and return a boolean:

let a = 5; let b = "5"; console.log(a == b); // Loose equality: true console.log(a === b); // Strict equality: false console.log(a != b); // Loose inequality: false console.log(a !== b); // Strict inequality: true console.log(a > 3); // Greater than: true console.log(a <= 5); // Less than or equal to: true

Logical Operators

These operators perform logical operations:

let isAdult = true; let hasLicense = false; console.log(isAdult && hasLicense); // Logical AND: false console.log(isAdult || hasLicense); // Logical OR: true console.log(!isAdult); // Logical NOT: false

Putting It All Together

Now that we've covered variables, data types, and basic operators, let's see how they work together in a practical example:

const username = "alice_wonder"; let age = 28; let isSubscribed = false; // Using template literals for string interpolation console.log(`Welcome, ${username}!`); if (age >= 18 && !isSubscribed) { console.log("You're eligible for a free trial subscription."); isSubscribed = true; } else if (age < 18) { console.log("Sorry, you must be 18 or older to subscribe."); } else { console.log("Thank you for being a subscriber!"); } // Using the ternary operator for concise conditionals let status = isSubscribed ? "Subscribed" : "Not subscribed"; console.log(`Subscription status: ${status}`);

This example demonstrates how variables, data types, and operators come together to create meaningful and interactive code. By understanding these fundamentals, you're well on your way to becoming proficient in JavaScript programming.

Popular Tags

javascriptvariablesdata-types

Share now!

Like & Bookmark!

Related Collections

  • JavaScript Interview Mastery: 20 Essential Concepts

    22/10/2024 | VanillaJS

  • JavaScript Coding Challenges for Interviews

    14/09/2024 | VanillaJS

  • JavaScript Mastery: From Basics to Advanced Techniques

    15/10/2024 | VanillaJS

Related Articles

  • Mastering JavaScript Fundamentals

    15/10/2024 | VanillaJS

  • Mastering Control Structures in JavaScript

    15/10/2024 | VanillaJS

  • Mastering Local Storage and Session Storage in Vanilla JavaScript

    15/10/2024 | VanillaJS

  • Mastering Asynchronous JavaScript

    15/10/2024 | VanillaJS

  • Understanding Rest and Spread Operators in Vanilla JavaScript

    22/10/2024 | VanillaJS

  • Unleashing the Power of WebAssembly in Vanilla JavaScript

    15/10/2024 | VanillaJS

  • Mastering Error Handling and Debugging in Vanilla JavaScript

    15/10/2024 | VanillaJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design