0% found this document useful (0 votes)
15 views5 pages

Crisostomo Assignment MC

The document provides a comprehensive overview of JavaScript, covering key concepts such as variables, data types, operators, and functions. It explains the definitions and characteristics of different variable types (var, let, const), outlines various data types (string, number, boolean, object, array, null, undefined), and describes different operators (arithmetic, comparison, logical). Additionally, it details the structure and usage of functions in JavaScript, emphasizing their importance in web development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Crisostomo Assignment MC

The document provides a comprehensive overview of JavaScript, covering key concepts such as variables, data types, operators, and functions. It explains the definitions and characteristics of different variable types (var, let, const), outlines various data types (string, number, boolean, object, array, null, undefined), and describes different operators (arithmetic, comparison, logical). Additionally, it details the structure and usage of functions in JavaScript, emphasizing their importance in web development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Jessica V.

Crisostomo
Course & Section: BSIT – 42M4
Subject: Mobile Computing

1. What are variables in JavaScript?


Definition of Variables:
 Variables are containers for storing data values in programming. They allow us to store
information, manipulate it, and perform operations on it within the program. Variables are
fundamental because they help store dynamic data that can change during the execution of a
program.
Why do we use variables?
o We use variables to hold data that can change during the program’s execution (such as
user input or results of calculations). Variables allow us to manage and manipulate data
easily without needing to repeat the same value multiple times.

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.

Comparison of var, let, and const:


Feature var let const

Function-scoped or globally
Scope Block-scoped Block-scoped
scoped

Re- Can be re-declared within its Cannot be re-declared in Cannot be re-declared in


declaration scope the same scope the same scope

Re- Cannot be reassigned


Can be reassigned Can be reassigned
assignment (immutable)

Hoisted to the top (undefined Hoisted but uninitialized Hoisted but uninitialized
Hoisting
until assignment) until assignment until assignment

2. Different Data Types in JavaScript


Overview of Data Types:
 Data types define the type of data a variable can hold. In JavaScript, the main data types are
primitive types and reference types. They are important because they determine how the
data is stored and manipulated.

List of Data Types:


1. String:
o A string is a sequence of characters enclosed in single (') or double (") quotes.
o Example:
o let greeting = "Hello, World!";
o console.log(greeting); // Output: Hello, World!
2. Number:
o The number type in JavaScript can represent both integers and floating-point numbers
(decimals).
o Examples:
o let age = 30; // Integer
o let price = 19.99; // Floating-point number
o console.log(age, price); // Output: 30 19.99
3. Boolean:
o A Boolean represents a logical entity that can either be true or false. It is often used in
conditionals and loops.
o Examples:
o let isActive = true;
o let isComplete = false;
o console.log(isActive, isComplete); // Output: true false
4. Object:
o An object is a collection of key-value pairs where the keys (properties) are strings and
the values can be any data type.
o Example:
o let person = {
o name: "John",
o age: 30,
o isStudent: false
o };
o console.log(person.name); // Output: John
5. Array:
o An array is an ordered collection of values. Arrays are zero-indexed and can hold
elements of any data type.
o Example:
o let colors = ["red", "green", "blue"];
o console.log(colors[0]); // Output: red
6. Null:
o null represents the intentional absence of any value or object. It is often used to indicate
that a variable has been explicitly set to have no value.
o Example:
o let person = null;
o console.log(person); // Output: null
7. Undefined:
o undefined is the default value of a variable that has been declared but has not been
assigned a value.
o Example:
o let name;
o console.log(name); // Output: undefined

Visual Aid:
Data Type Example Description

String "Hello" Sequence of characters

Number 42, 3.14 Integer or floating-point value

Boolean true, false Logical value (either true or false)


Data Type Example Description

Object { name: "John", age: 30 } Collection of key-value pairs

Array [1, 2, 3, 4] Ordered list of values

Null null Represents no value or object

Undefined undefined Variable not assigned a value

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)

4. What is a function in JavaScript?


Definition of Functions:
 A function is a block of code designed to perform a particular task. It can take input
(parameters), perform operations, and return a result (return value).
Defining a Function:
 Syntax:
 function functionName(parameters) {
 // code to execute
 }
Example:
function greet(name) {
console.log("Hello, " + name + "!");
}
Calling a Function:
 Syntax:
 functionName(arguments);
Example:
greet("John"); // Output: Hello, John!
Parameters and Return Values:
 A function can take parameters (inputs) and return a value.
 Example with parameters and return value:
 function add(a, b) {
 return a + b;
 }
 console.log(add(5, 3)); // Output: 8

This comprehensive guide provides an overview of JavaScript variables, data types, operators, and
functions, all crucial concepts for web development.

You might also like