JavaScript Basics: Variables and Functions

Sep 18, 2024

Full Stack Capstone Assignment Notes

Introduction

  • Presenter: Rakesh, 2nd year CSC
  • Topic: JavaScript Variables, Functions, Objects, and Loops

JavaScript Variables

  • Keywords for Declaring Variables: var, let, and const
    • var:
      • Can be redeclared and updated.
      • Example: Variable redeclaration and reassignment.
    • let:
      • Can be updated but not redeclared.
      • Example: Redeclaring variable A results in an error, but updating is allowed (e.g., A = 200).
    • const:
      • Cannot change its value after declaration.
      • Example: Changing the value of a const variable results in an error.

Arrays

  • Arrays can store multiple elements of similar data types.
  • Manipulation of arrays using indexes:
    • Example: Change element at index 0 to 100 and print.
    • Individual elements can be printed using their index.

Functions

  • Used to execute a block of code multiple times.
  • Example: Function to multiply numbers by 3.
    • Declare a number, call the function with different inputs.
    • Output shows the result after multiplication.

Objects

  • Create a file objects.js.
  • Access properties using:
    • Dot Notation: e.g., dog.legs
    • Bracket Notation: e.g., dog['tails']
  • Updating object properties:
    • Example: Change dog.mode to a new value and print.

Loops

  • Create a file loops.js.
  • Types of loops: while, do while, for.
    • While Loop: Code to print the first five natural numbers.
    • For Loop: Code to print numbers from 1 to 5.
    • Do While Loop: Similar implementation with a different structure.

Conclusion

  • Code examples demonstrate the use of variables, arrays, functions, objects, and loops in JavaScript.