CSE3150 - Front-end Full Stack Development Module 2 Notes
Module 2: Responsive Web Design and JavaScript Basics
Introduction to JavaScript
Definition: JavaScript is a lightweight, high-level programming (scripting) language that enables interactivity and dynamic behavior in web pages, making them more engaging and user-friendly.
Features:
Dynamic Text: JavaScript allows developers to insert dynamic text into HTML documents, which can change based on user interaction, such as displaying personalized messages or real-time data updates.
Event Handling: It reacts to various events like page loads, clicks, and keyboard entries, enabling developers to create responsive user interfaces.
User Information Collection: It can gather and process information about the user's hardware and software environment, including browser type and screen resolution.
Client-Side Computation: JavaScript can perform calculations and validations directly on the user's device, such as checking form inputs before submission, enhancing performance by reducing server load.
JavaScript vs Java
Key Differences:
Interpretation vs Compilation: JavaScript is an interpreted language, meaning it is executed line by line at runtime, while Java is a compiled language that must be pre-compiled into bytecode.
Syntax Flexibility: JavaScript has a more relaxed syntax compared to Java, allowing for dynamic typing and fewer restrictions on variable declarations.
Function vs Class-Based: Functions in JavaScript are treated as first-class constructs, which enables functional programming techniques such as passing functions as arguments or returning them from other functions. In contrast, Java is primarily class-based and object-oriented.
Integration: JavaScript can be easily embedded within HTML/CSS, allowing for seamless integration and modification of web content dynamically without needing to reload the page.
Linking JavaScript to HTML
Implementation: JavaScript can be linked or included in HTML documents using
<script>tags:It is common to place
<script>tags in the<head>for scripts that control the initial page behavior or at the end of the body for scripts that need to load after the content is fully rendered.Example:
<script src="filename.js" type="text/javascript"></script>includes an external JavaScript file in the HTML.
Event-Driven Programming
JavaScript programming is designed to respond to user interactions (events) which makes it inherently event-driven.
Common event types include user actions such as mouse clicks, key presses, and form submissions.
Developers can assign JavaScript functions as event handlers, allowing specific blocks of code to execute in reaction to events.
Example: A button might have an
onclickattribute that triggers a specific function to run whenever the button is clicked, enhancing user interaction.
Document Object Model (DOM)
Definition: The Document Object Model (DOM) is a programming interface that represents the structure of HTML documents, making it possible to manipulate the content and structure of pages.
Capabilities:
JavaScript can dynamically change HTML element content, attributes, and styles, allowing for real-time updates to the user interface.
It can create, delete, and modify elements or attributes in a document, facilitating advanced UI functionality like modals, pop-ups, and interactive forms.
JavaScript can react to user-defined events in the existing HTML structure, enhancing the interactive behavior of web applications.
Accessing Elements:
The method
document.getElementById("id")is used to access specific elements by their unique ID.Content can be modified using
innerHTML, or styles can be directly altered through theelement.styleproperty.
JavaScript Functions
Definition: Functions are foundational building blocks in JavaScript, allowing for code reuse and modularization.
Syntax:
Example:
function functionName() { statements; }defines a function, and its invocation can occur through user events or other code execution.
JavaScript Output Methods
JavaScript provides several methods for outputting data to users or for logging information during development:
innerHTML: This method modifies the content of DOM elements directly, allowing for dynamic updates based on user actions.document.write(): This writes directly to the document stream, but caution is advised as it can overwrite the entire page if used after rendering is complete.window.alert(): This pops up an alert box with a message, providing a basic way to communicate with users.console.log(): This outputs messages to the browser's console, useful for debugging and monitoring application status without interfering with the user experience.
JavaScript Variables
Declaration: Variables can be declared using
var,let, orconst, whereletandconstprovide block scope, whilevarhas function scope.Example:
let age = 25;declares a variable namedageand assigns it an initial value of 25.Data Types:
Primitive Data Types: Include String, Number (64-bit float), Boolean, Null, and Undefined.
Loose Typing: JavaScript is loosely typed, meaning that variables can hold any data type and can change types during runtime based on what value is assigned.
Operators in JavaScript
Logical Operators:
Logical AND:
&&Logical OR:
||Logical NOT:
!Comparison Operators: Include
==,!=,===, and!==, with the latter two providing type-safe comparisons.Arithmetic Operators: Include
+,-,*,/, and%, which are commonly used for mathematical calculations.
Control Structures
JavaScript supports various control structures, including:
Conditional Statements: Employs if/else structures that allow for decision-making based on logical conditions.
Loops: Encompasses
for,while, anddo...whileloops, enabling repetitive execution of code blocks.Iteration:
for...inandfor...ofloops allow for iterating through object properties and array elements, respectively, providing a streamlined approach to working with collections.
JavaScript Arrays and Objects
Arrays: Serves as collection structures that can hold multiple values (e.g., numbers, strings), and can be manipulated with various methods such as
push(),pop(),shift(), andunshift().Objects: Formed as collections of key-value pairs, allowing representation of complex data and real-world entities.
Example:
const person = {name: "John", age: 30};illustrates an object with properties representing a person.
Comments in JavaScript
Syntax:
Single-line Comments: Use
// commentfor brief explanations.Multi-line Comments: Enclose comments with
/* comment */for longer descriptions, similar to Java syntax.
These notes cover the core foundational aspects of JavaScript as introduced in the course CSE3150 at Presidency University, focusing on syntax, functionality, and web integration principles, providing a comprehensive overview for learners to grasp essential concepts and applications of JavaScript in web development.