Skip to content

NomthaPR/JavaScript-notes-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

JavaScript-notes-

AFTER LEARNING HTML, CSS AND JAVASCRIPT HTML (HyperText Markup Language): This is the basic structure of a website. It uses tags to identify different types of content and their purposes on a webpage. Each content element is enclosed within HTML tags. HTML provides the raw tools needed to structure content on a website.

CSS (Cascading Style Sheets): CSS is a programming language that dictates how HTML elements should appear on the front end of a webpage. It styles the content to give the website its visual appeal, including colors, fonts, layout, and background images. CSS affects the mood and tone of a webpage, making it an essential tool for web developers. It also enables websites to adapt to different screen sizes and device types.

JavaScript: JavaScript is a logic-based programming language used to modify website content and make it behave in different ways in response to user actions. It adds dynamic behavior to a webpage, allowing for actions such as confirmation boxes, calls-to-action, and updating information on the fly. JavaScript is essential for handling requests and responses on a website, storing information, and creating interactive user experiences.

In summary, HTML provides the structure, CSS styles the content, and JavaScript adds interactivity and dynamic behavior to web pages. Together, these languages form the foundation of web development and are essential skills for anyone building websites.

TYPES CONVERSIONS IN JAVASCRIPT

In JavaScript, automatic type conversion occurs in many cases, where operators and functions automatically convert values to the appropriate type based on the context. For instance: Alert Function: The alert() function automatically converts any value to a string in order to display it as a message. Mathematical Operations: Mathematical operations typically convert values to numbers. For example, if you perform addition on two values, JavaScript will convert them to numbers before adding them together. While automatic type conversion is common, there are scenarios where you might need to explicitly convert a value to a specific type, particularly when dealing with string conversion. For string conversion, you can use the String() function explicitly. For example:

javascript let value = 42; let stringValue = String(value); console.log(stringValue); // Outputs: "42" Here, String(value) explicitly converts the number 42 to a string "42". Explicit type conversion ensures that values are converted to the expected type, which can be crucial for proper functionality in your code.

JavaScript Numeric conversion happens automatically in mathematical functions and expressions. When mathematical operations, such as division (/), are applied to non-numeric values, JavaScript will attempt to convert those values into numbers before performing the operation.

For example: let result = "10" / 2; console.log(result); // Outputs: 5 In this example, the string "10" is automatically converted to the number 10 before the division operation is performed, resulting in 5. other mathematical operations like addition (+), subtraction (-), and multiplication (*) will also trigger automatic numeric conversion if the operands are not already numbers. This automatic conversion helps JavaScript handle various situations where different data types are used in mathematical expressions, ensuring consistency and enabling mathematical operations to be performed effectively.

Numeric conversion rules: 0: Stays as 0. null: Converts to 0. NaN: Stays as NaN. If it's already a NaN, it remains unchanged. undefined: Converts to NaN (Not a Number), which represents an undefined or unrepresentable value in JavaScript arithmetic. true and false: Convert to 1 and 0, respectively. This is particularly useful when performing arithmetic operations involving boolean values.

Strings: Leading and trailing whitespaces are removed. If the remaining string is empty or contains only whitespaces, it converts to 0. Otherwise, if the string represents a valid number (with optional signs, digits, decimal points, and exponents), JavaScript reads the number from the string. If the string cannot be parsed as a valid number (e.g., contains non-numeric characters other than whitespaces), it results in NaN.

Boolean conversion in JavaScript follows a simple rule: Values that are considered "empty" or falsy, such as 0, an empty string "", null, undefined, and NaN, become false. Other values, including non-zero numbers, non-empty strings, objects, arrays, functions, etc., become true.

For example: alert(Boolean(0)); // Outputs: false alert(Boolean("hello")); // Outputs: true alert(Boolean("")); // Outputs: false alert(Boolean(1)); // Outputs: true It's worth noting that in JavaScript, unlike some other languages like PHP, a non-empty string, even if it contains "0", is considered true. So, "0" evaluates to true: alert(Boolean("0")); // Outputs: true Understanding boolean conversion is essential for writing logical operations and conditional statements effectively in JavaScript.

The Document Object Model and JavaScript Syntax (Application programming interface) The Document Object Model (DOM) serves as an essential interface for HTML and XML documents in web development. It offers two main functionalities: Structural Representation: The DOM provides a structured representation of the document, defining elements, attributes, and their relationships within the document tree. Access from Script: It specifies how this structured representation can be accessed and manipulated from scripting languages such as JavaScript. This enables developers to interact with and modify the content, structure, and styling of web pages dynamically. By leveraging the DOM, web developers can programmatically access and manipulate various elements and attributes of HTML documents, enabling dynamic and interactive web experiences. In JavaScript, accessing objects, properties, and methods typically involves using the "dot syntax." This means that to access a property or method of an object, you specify the object's reference followed by a dot (.) and then the name of the property or method. This syntax helps organize and navigate through complex object structures.

The information provided offers a comprehensive overview of JavaScript objects, properties, methods, and the Document Object Model (DOM) commonly used in web development. JavaScript Objects: In JavaScript, an object can be any scriptable HTML element, including the window object, which represents the browser window. Objects can be associated with HTML elements, core language objects, or homemade objects. Common JavaScript objects include window, document, form, and image.

Properties: Objects in JavaScript have properties, which are characteristics of the object. Properties can be simple values or objects themselves. JavaScript properties can be accessed using dot notation (object.property), similar to HTML tag attributes.

Methods: Methods are actions that can be applied directly to objects. They are signified by parentheses following their names and can take parameters. Common JavaScript methods include alert(), write(), and focus().

Core APIs in the DOM: document and window objects are commonly used in DOM programming. document represents the root of the document, while window represents the browser window. Methods and properties provided by these objects are frequently used for accessing and manipulating elements on a webpage.

Example APIs in DOM programming: document.getElementById(id): Retrieves an element by its ID. document.getElementsByTagName(name): Retrieves elements by their tag name. document.createElement(name): Creates a new element with the specified tag name. element.innerHTML: Gets or sets the HTML content of an element. element.style.left: Gets or sets the value of the left CSS property of an element. element.addEventListener: Attaches an event listener to an element. window.onload: Event handler triggered when the window loads.

DOM Structure: The window object sits at the top of the object hierarchy and represents the browser window. The document object is contained within the window object and represents the document itself. Most JavaScript paths reference the document object explicitly, while the window object is often assumed and omitted. Understanding these concepts is fundamental for effective web development with JavaScript and the DOM. They enable developers to interact with and manipulate web page elements dynamically, creating rich and interactive user experiences.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published