Crisostomo Assignment MC
Crisostomo Assignment MC
Crisostomo
Course & Section: BSIT – 42M4
Subject: Mobile Computing
Types of Variables:
1. var:
o Characteristics of var:
var is function-scoped or globally scoped (if declared outside of any function).
Variables declared with var can be re-declared and updated within their scope.
It is a bit outdated and has been largely replaced by let and const due to issues
like hoisting and scoping.
o When to use var:
var is still used in legacy code, but let and const are preferred for modern
JavaScript development.
o Example:
o var age = 30; // Declare a variable using 'var'
o console.log(age); // Output: 30
2. let:
o Characteristics of let:
let is block-scoped, meaning it is only available within the block (such as inside a
loop or a conditional statement) in which it is defined.
let allows variables to be reassigned, but not re-declared within the same scope.
It solves some of the issues of var, such as scope leakage.
o When to use let:
Use let when you expect the value of the variable to change during execution,
and you want the scope of the variable to be limited to the block it is in.
o Example:
o let name = "John"; // Declare a variable using 'let'
o name = "Doe"; // Reassign a new value
o console.log(name); // Output: Doe
3. const:
o Characteristics of const:
const is block-scoped, similar to let.
Once a variable is assigned with const, it cannot be reassigned (immutable).
const is used to define constants, where the value will not change after
initialization.
o When to use const:
Use const when you know the variable’s value should remain constant
throughout the program (e.g., for constants or fixed references).
o Example:
o const pi = 3.14159; // Declare a constant using 'const'
o console.log(pi); // Output: 3.14159
o // pi = 3.14; // This would throw an error because pi is a constant.
Function-scoped or globally
Scope Block-scoped Block-scoped
scoped
Hoisted to the top (undefined Hoisted but uninitialized Hoisted but uninitialized
Hoisting
until assignment) until assignment until assignment
Visual Aid:
Data Type Example Description
3. Operators in JavaScript
Definition of Operators:
Operators in programming are symbols used to perform operations on variables and values.
They help in calculations, comparisons, and logical decision-making in programs.
Types of Operators:
1. Arithmetic Operators:
o Used to perform mathematical operations such as addition, subtraction, multiplication,
etc.
o Examples:
o let a = 10, b = 5;
o console.log(a + b); // Output: 15 (Addition)
o console.log(a - b); // Output: 5 (Subtraction)
o console.log(a * b); // Output: 50 (Multiplication)
o console.log(a / b); // Output: 2 (Division)
2. Comparison Operators:
o Used to compare values. They return true or false.
o Examples:
o let a = 10, b = 5;
o console.log(a == b); // Output: false (Equal to)
o console.log(a === b); // Output: false (Strict equal to)
o console.log(a > b); // Output: true (Greater than)
o console.log(a < b); // Output: false (Less than)
3. Logical Operators:
o Used to combine multiple conditions in logical expressions.
o Examples:
o let a = true, b = false;
o console.log(a && b); // Output: false (AND)
o console.log(a || b); // Output: true (OR)
o console.log(!a); // Output: false (NOT)
This comprehensive guide provides an overview of JavaScript variables, data types, operators, and
functions, all crucial concepts for web development.