JavaScript Objects
What is an Object? An object is a collection of related data and functionalities, organized as properties and methods. Properties […]
What is an Object? An object is a collection of related data and functionalities, organized as properties and methods. Properties […]
What is a Function? A function is a block of code designed to perform a particular task. Functions are executed
Arithmetic Operators Arithmetic operators are used to perform mathematical calculations. Assignment Operators Assignment operators are used to assign values to
Primitive Data Types JavaScript has several primitive data types that represent simple values. 1. Number The number data type represents
What are Variables? Variables are used to store data that can be referenced and manipulated in a program. They act
Single-Line Comments Single-line comments are used to comment out a single line of code. They begin with //. Explanation of
What are JavaScript Statements? JavaScript statements are commands that tell the browser what to do. They are the building blocks of JavaScript code and are executed in the order they are written. Statements often end with a semicolon (;), although it’s not always mandatory. Assigment Statement An assignment statement assigns a value to a variable. Function Call Statement A function call statement invokes a function. Conditional Statement A conditional statement executes code based on a condition. Loop Statement A loop statement repeats a block of code as long as a specified condition is true. JavaScript Code
JavaScript Statements JavaScript statements are instructions to be executed by the browser. They can perform actions like displaying output, making calculations, or manipulating the DOM. Each statement is usually separated by a semicolon (;). Explanation of Code: Each line in the example above is a JavaScript statement. The last statement uses console.log to output the value of z. Case Sensitivity JavaScript is case-sensitive, meaning myVariable and myvariable are considered two different variables. Explanation of Code: The example demonstrates that myVariable and myvariable are treated as separate variables. Whitespace and Indentation JavaScript ignores extra spaces, tabs, and newlines, which means you can format your code for better readability without affecting its execution. Exaplanation of Code:
Using innerHTML The innerHTML property is used to change the HTML content of an element. Explanation of Code: The changeContent function changes the content of the <p> element with the ID demo to “Content has been changed!” when the button is clicked. Using document.write() The document.write() method writes directly to the HTML document. It’s typically used for testing purposes and not recommended for production. Explanation of Code: The document.write method writes the text “This is written directly to the document.” to the HTML document. Using alert() The alert() method displays a message in an alert box. Explanation of Code: The showAlert function displays an alert box with the message “This is an alert box!” when the button is clicked. Using console.log() The console.log() method is used to display output in the browser’s console, which is useful for debugging.
Inline JavaScript Inline JavaScript is written directly within an HTML element’s attribute. This method is typically used for small snippets of code. Explanation of Code: In this example, the JavaScript code is placed within the onclick attribute of the <button> element. When the button is clicked, an alert box displays the message “Hello, World!”. Internal JavaScript Internal JavaScript is written within the <script> tag, inside the HTML file. It can be placed in the <head> or <body> section of the document. Explanation of Code: The JavaScript code is placed inside a <script> tag within the <head> section. The function showMessage is defined, and it is called when the button is clicked. External JavaScript External JavaScript is written in a separate .js file, which is then linked to the HTML document. This method keeps the HTML and JavaScript code separate, making the code easier to maintain. Explanation of Code: