DIGITAL UNIVERSITY OF CAMBODIA
JAVASCRIPT
Faculty Of Digital Industry
Dariya Thorn
T
I
T
L
E
01
1.Introduction to JavaScript
2.Variables and Data Types
3.Syntax and Statements
4.Objects and Arrays
5.Document Object Model
6.Debugging and Error Handling
7.Modern JavaScript
8.Working with JSON
9.Modules and Functions
មេម ៀនទី
១
មេម ៀនទី៣
Syntax and Statements
Agenda
1.What JavaScript Syntax and statement.
2.Types of Syntax and Statements
3.What did you learn about this lesson.
JavaScript syntax comprises a set of rules that define
how to construct a JavaScript code.
JavaScript statements are programming instructions
that a computer executes.
syntax and statements is fundamental to writing
effective programs.
What JavaScript Syntax and statement
1.Declaration Syntax
2.Expression Syntax
3.Assignment Syntax
4.Control Flow Syntax
5.Object and Array Syntax
6.Function Syntax
7.Class Syntax
8.Exception Handling Syntax
9.Module Syntax
10.Asynchronous Syntax
11.Template Literals Syntax
Type of Syntax in JavaScript
1.Declaration Syntax
Type of Syntax in JavaScript
•Variable Declaration:
Example
let name = "Dariya";
const age = 23;
var city = "C";
•Function Declaration:
Example
function greet()
{
console.log("My name is dariya");
}
1.Expression Syntax
Type of Syntax in JavaScript
An expression is any valid unit of code that resolves to a value.
• Arithmetic Expressions:
Example
let sum = 5 + 3;
let product = 4 * 2;
• String Concatenation:
Example
let message = "Hello" + " " + "World!";
• Logical Expressions:
Example
let isAdult = age > 18 && age < 60;
1.Assignment Syntax
Type of Syntax in JavaScript
Used to assign values to variables or constants.a value to a
variable or constant using the assignment operator (=).
Example
let x = 10;
x += 5;
1.Control Flow Syntax
Type of Syntax in JavaScript
Used to dictate the flow of the program by making decisions or
repeating code.
Examples
Conditional statements:
if (age > 18) {
console.log(“Hello");
} else {
console.log(“Hi"); }
Loops:
for (let i = 0; i < 4; i++)
{ console.log(i); }
objects (key-value pairs) and arrays (collections of items).
Allows you to store and manage structured data.
Examples:Objects:
let person = { name: "Alice", age: 30 };
Examples: Arrays:
let fruits = ["apple", "banana", "cherry"];
Type of Syntax in JavaScript
1.Object and Array Syntax
Type of Syntax in JavaScript
1.Function Syntax
Functions are reusable blocks of code designed to perform a
task.
Defines how a function is created and used in JavaScript.
Examples:
Regular function:
function sum(a,b){
return a+b;
}
console.log(sum(2, 5));
Arrow function:
const sum = (a, b) => a + b;
Type of Syntax in JavaScript
1.Class Syntax
A blueprint for creating objects with predefined properties and
methods.
It simplifies object creation and inheritance
Example:
class Fruit {
constructor(name, color) {
this.name = name;
this.color = color;
} }
const fruit = new Fruit("Apple", "Red");
console.log(`Fruit Name: ${fruit.name}, Color: ${fruit.color}`);
Type of Syntax in JavaScript
1.Exception handling Syntax
exception handling is the procedure/method used to handle the
abnormal behavior of our JavaScript applications.It ensures the
program gracefully handles errors or exceptions.
Example:
try {
let result = functionName();
console.log(result);
} catch (error) {
console.log("An error occurred: " + error.message); }
finally {
console.log("Cleanup actions."); }
Type of Syntax in JavaScript
1.Module Syntax
Module is a function or group of similar functions. They
are grouped together within a file and contain the code to
execute a specific task when called into a larger
application.
Example: main.js
Type of Syntax in JavaScript
Example: quiz.js
Type of Syntax in JavaScript
1.Asynchronus Syntax
Asynchronous programming allows code to
run without blocking other operations.
➢Async :Marks a function to always return a Promise.
➢Await :Pauses the execution of the async function until the
Promise is resolved or rejected.
Type of Syntax in JavaScript
Example:
Template literals are string literals that allow embedded
expressions (variables) into your code. They are enclosed by
backticks (`) instead of single (‘) or double (“) quotes.
Type of Syntax in JavaScript
1.Template Literal Syntax
➢Backticks (`): Used to define a template literal.
➢${}: Used to insert expressions inside the string, such as
variables, function calls, or arithmetic operations.
Types of Statementsin JavaScript
1. Variable Declarations (var, let, const)
In JavaScript, you can declare variables usingvar, let, or const. let
and const are used in modern JavaScript for block-scoped
variables, while var is function-scoped and generally considered
outdated.
Example
let name = "Mohan";
const age = 25;
var isActive = true;
2. Assignment Statement
An assignment statement is used to assign a value to a variable.
You can assign any type of value, including strings, numbers, and
objects, using the assignment operator =.
Example
let number = 10;
let message = "Hello, World!";
Types of Statementsin JavaScript
3. Expression Statements
Anexpressionin JavaScript is any valid unit of code that resolves to a
value. When an expression is executed, it can perform an action (e.g.,
calculation, function call) and return a result. An expression can also
be used as a statement.
Example
Let y = 2;
x = y + 10;
console.log(x);
Types of Statementsin JavaScript
4. Control Flow Statements
Control flow statementsare used to control the order in which
statements are executed in a program.
Examples include if, else, switch, while, and for loops.
Example
let number = 10;
if (number > 5) {
console.log("Number is greater than 5");
}
Types of Statementsin JavaScript
Types of Statementsin JavaScript
5. Function Declarations
A function declaration is a statement that defines a function in
JavaScript. Functions are reusable blocks of code designed to
perform specific tasks.
Example
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alisha"));
Types of Statementsin JavaScript
6. Return Statement
Thereturn statementis used to exit a function and optionally
pass a value back to the calling code.
Example
function add(x, y) {
return x + y;
}
let result = add(5, 3);
Types of Statementsin JavaScript
7. Throw Statement
Thethrow statementis used to create custom errors in
JavaScript. It is often used in conjunction with try…catch to
handle errors.
Example
function checkAge(age) {
if (age < 18) {
throw new Error("Age must be 18 or older");
} }
Types of Statementsin JavaScript
8. Try…Catch Statement
Thetry…catchstatement is used to handle exceptions in
JavaScript. The code inside the try block is executed, and if an
error occurs, the code inside the catch block will handle the error.
Example
try {
let result = someUndefinedFunction();
console.log(result);
} catch (error) {
console.error("An error occurred:", error.message);
}
Types of Statementsin JavaScript
9. Break and Continue Statements
Thebreak and continuestatements are used within loops. break
exits the loop, while continue skips to the next iteration.
Example
for (let i = 0; i < 10; i++) {
if (i === 5) {
break
}
console.log(i);
}
Result= 0,1,2,3,4
What did you learn about this lesson?
Summary the lesson