Wednesday, April 23, 2025

JavaScript Switch Statement

A switch statement is a control flow statement used in programming that allows a value to be tested against a list of cases. It is similar to an if-else statement but is more concise and can be easier to read for certain scenarios.

The switch statement takes an expression as an input, which is evaluated against multiple cases. Each case consists of a specific value or a range of values to compare the expression against. If the expression matches a case, the statements associated with that case are executed. If there is no match, a default case is executed (if provided). 

We need break statements in switch statements to ensure that only the code corresponding to the matching case statement is executed and the execution of the switch statement is terminated. Without a break statement, execution will "fall through" to the next case statement and possibly execute that code as well. This can lead to unexpected behavior and bugs in the program.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var option = "a";
	
	switch(option) {
		case "a":
			document.write("User needs A");
			break;
		case "b":
			document.write("User needs B");
			break;
		case "c":
			document.write("User needs C");
			break;
		default:
			alert("I am from default");	
	}
		
</script>
	
</body>
</html>

This is code that uses a switch statement to check the value of the variable option. The switch statement tests the value of the expression (option) against multiple cases.

In this example, the value of option is "a". The switch statement checks the value of option against three cases: "a", "b", and "c". Since option matches the first case, "a", the code inside that case is executed. In this case, it writes the message "User needs A" to the document using document.write().

After the case is executed, the break statement terminates the switch statement, preventing execution of the other cases. If none of the cases match, the default case is executed. In this example, it displays an alert message "I am from default".

Switch statements are useful in situations where you need to evaluate a variable or an expression against a series of possible values and take different actions based on the value. They can help simplify code and improve performance compared to using multiple if-else statements.

For example, switch statements are commonly used in menu systems, where different options may trigger different actions based on the user's selection. They are also used in data processing and analysis, where different types of data may require different processing logic.

The default keyword is used to define the default case that will be executed if none of the cases match the input value. It is similar to the else clause in an if-else statement.

If none of the cases match the input value, the code in the default block will be executed. The default case is optional, and it is not necessary to include it in a switch statement. However, it is considered good practice to include a default case to handle unexpected input values.

JavaScript If inside If

Nested if statements are if statements that are placed within another if statement. They can be useful in certain situations where there are multiple conditions that need to be checked, and the execution of a code block should depend on the outcome of those conditions.

For example, when implementing complex authentication systems or nested menu structures, nested if statements can help to create a logical and organized structure for the code.

However, it is important to use nested if statements with caution, as they can quickly become complex and difficult to manage.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var name = "Michael";
	var pass = "hackthe1planet";
	
	if (name == "Michael") {
		if (pass == "hacktheplanet") {
			document.write("Access Granted");
		}
	} else {
		document.write("Login Failed");
	}
		
</script>
	
</body>
</html>

This code demonstrates a nested if statement, where there is an if statement inside another if statement. In this case, the outer if statement checks if the name variable is "Michael". If that is true, then the inner if statement checks if the pass variable is "hacktheplanet".

If both conditions are true, then the message "Access Granted" is printed. If the pass variable is not "hacktheplanet", the message "Wrong password" is printed.

If the name variable is not "Michael", the message "Login Failed" is printed. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var name = "Michael";
	var pass = "hacktheplanet";
	
	if (name == "Michael") {
		if (pass == "hacktheplanet") {
			document.write("Access Granted");
		} if (pass != "hacktheplanet") {
			document.write("Wrong Password");
		}
	} else {
		document.write("Login Failed");
	}
		
</script>
	
</body>
</html>

The code is an example of a nested if statement. It first checks if the variable name is equal to the string "Michael". If that condition is true, it then checks if the variable pass is equal to the string "hacktheplanet". If that condition is also true, it prints "Access Granted".

It is not necessarily bad to use nested if statements in programming, as there may be situations where it is the most clear and concise way to express a conditional statement. However, it is generally recommended to keep code as simple and easy to read as possible.

If statements nested too deeply can quickly become difficult to read and understand, especially if they involve complex conditions or multiple levels of nesting. It is often better to use logical operators like && and || to combine conditions into a single if statement, or to refactor the code into smaller functions or modules that each perform a specific task.

Ultimately, the decision to use nested if statements should be based on the specific situation and the best way to express the intended logic in a clear and readable way.

JavaScript If Statement

In programming, an if statement is a conditional statement used to execute a block of code if a certain condition is true.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var number = 100;
	
	if (number < 100) {
		alert("Number is less then 100");
	}
	if (number == 100) {
		document.write("They are same");
	}	
		
</script>
	
</body>
</html>

This code defines a variable called number with a value of 100, and then uses two if statements to check its value.

The first if statement checks whether number is less than 100. Since number is equal to 100, this condition is not true, and the code inside the block (alert("Number is less than 100");) is not executed.

The second if statement checks whether number is equal to 100. Since number is indeed equal to 100, the code inside the block (document.write("They are same");) is executed, and the text "They are same" is written to the document. 

Parentheses () are used to group expressions and determine their evaluation order, while curly braces {} are used to group statements together to form a block of code.

In the case of an if statement, the parentheses are used to enclose the condition or expression that is being evaluated. The condition can be a simple comparison, or a more complex expression involving logical operators such as && (AND) or || (OR). The braces are used to enclose the statements that should be executed if the condition is true. By using braces, we can group multiple statements together, ensuring that they are all executed if the condition is true.

It is good practice to always use braces with an if statement, even if there is only a single statement to be executed. This helps avoid errors and ensures that the code is more readable and maintainable.

A block of code is a section of program statements that are grouped together and treated as a single unit. A block is usually delimited by curly braces {} and can contain any number of statements, including other blocks of code. Blocks are used to group statements together so that they can be executed together as a unit, or to create a new scope for variables and functions declared inside the block. Blocks can also be used to create conditional code that only executes if a certain condition is met, such as with an if statement or a loop.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var number = 123;
	
	if (number < 100) {
		alert("Number is less then 100");
	}
	if (number == 100) {
		document.write("They are same");
	}
	if (number > 100) {
		alert("Our real number is bigger than starting number");
	}	
		
</script>
	
</body>
</html>

The code defines a variable number with a value of 123, and then uses a series of if statements to check its value.

The first if statement checks if number is less than 100, which is not true, so the code inside the statement will not be executed.

The second if statement checks if number is equal to 100, which is also not true, so the code inside the statement will not be executed.

The third if statement checks if number is greater than 100, which is true, so the code inside the statement will be executed and an alert message will appear saying "Our real number is bigger than starting number".

JavaScript Numbers, Math Operations

JavaScript supports mathematical operations such as addition, subtraction, multiplication, division, and more. These operations can be performed using the corresponding arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division, % for modulus (remainder), and ** for exponentiation. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function allofthem(x, y) {
		document.write("Div: " + (x / y) + "<br>");
		document.write("Mul: " + (x * y) + "<br>");
		document.write("Sub: " + (x - y) + "<br>");
		document.write("Add: " + (x + y) + "<br>");
		
		//Remainder of division:
		document.write("Mod: " + (x % y) + "<br>");
	}
	
	allofthem(10, 3);
		
</script>
	
</body>
</html>

This code defines a function called allofthem which takes two arguments x and y. Within the function, there are several arithmetic operations being performed using these two arguments. Specifically, it performs division (x / y), multiplication (x * y), subtraction (x - y), addition (x + y), and finds the remainder of division (x % y).

After defining the function, it is called with the arguments 10 and 3, which will output the results of these arithmetic operations for those particular values. The results are printed to the web page using document.write(), which writes the specified text (in this case, the results of the operations) to the page.

The modulo operation calculates the remainder of the division of two numbers. In JavaScript, the modulo operator is represented by the percent sign (%). For example, 10 % 3 would give the result of 1 because 10 divided by 3 is 3 with a remainder of 1. So, the modulo operation returns the remainder of 10 divided by 3.

JavaScript Global vs Local Variables

Global variables are those that are declared outside of any function and are accessible throughout the entire program, while local variables are those that are declared within a function and are only accessible within that function.

Global variables are often used to store information that needs to be accessed by multiple functions or throughout the entire program. However, excessive use of global variables can lead to potential issues, such as naming conflicts between different functions or unexpected changes to the value of the variable from unintended sources.

On the other hand, local variables are only accessible within the function they are declared in, making them more secure and less prone to naming conflicts. Local variables are typically used to store temporary values that are only needed within a specific function.

It's important to note that when a local variable and a global variable have the same name, the local variable takes precedence within the scope of the function. This is known as variable shadowing, where the local variable "shadows" the global variable of the same name. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var number = 456;
	
	function printing() {
		var number = 777;
		
		document.write("Local Variable: " + number);
		document.write("<br><br>");
	}
			
	printing();
	
	document.write("Global Variable: " + number);
		
</script>
	
</body>
</html>

In this script, there is a global variable named number with a value of 456. Then there is a function named printing() that defines a local variable with the same name number and assigns it a value of 777.

When the function is called, it will execute and print the value of the local variable number which is 777. After the function is executed, the script will continue to execute and print the value of the global variable number which is 456.

This demonstrates the concept of local vs. global variables. Local variables are defined within a function and are only accessible within that function, while global variables are defined outside of functions and are accessible throughout the entire script. If a local variable has the same name as a global variable, the local variable takes precedence within that function, while the global variable remains unchanged.

JavaScript Endless Loop, Infinite Loop

An infinite loop in programming is a loop that never terminates, meaning it continues to execute indefinitely. This can occur for a variety of reasons, such as an incorrect or missing loop termination condition, or a logical error in the loop body that prevents the condition from being met.

Infinite loops are generally considered bad because they can cause a program to crash or become unresponsive, consuming valuable system resources and potentially causing data loss or corruption. They can also cause performance issues, as the program may be stuck in the loop and unable to complete other tasks or respond to user input.

In some cases, infinite loops may be intentional, such as in server-side applications that need to continually listen for incoming connections. However, in most cases, it is important to ensure that loops have a well-defined termination condition to avoid infinite loops and their associated problems. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function first() {
		document.write("This is from First Function <br>");
		second();
	}
	
	function second() {
		document.write("This is from Second Function <br>");
		first();
	}
	
	function caller() {
		document.write("----------------<br>");
		first();
		second();
	}
	
	caller();
	
</script>
	
</body>
</html>

This is infinite loop example. The functions first() and second() call each other recursively, meaning they call themselves over and over again without any stopping condition. As a result, the caller() function that invokes both first() and second() also goes into an infinite loop.

Infinite loops are generally bad because they can cause a program to become unresponsive, crash or consume a lot of resources such as memory or CPU time. This can lead to poor performance or even render the program unusable. It's important to make sure that loops and recursion have a stopping condition to prevent infinite looping.

In general, infinite loops do not cause direct damage to hardware. They are a software issue that can cause a program to become unresponsive and consume excessive CPU resources, which can slow down other processes running on the same system.

However, if a program is using hardware resources in a way that is not intended, it could potentially cause damage.

For example, if a program repeatedly writes data to a hard disk without proper error checking, it could cause the disk to fail. It's important to note that infinite loops are not necessarily malicious or harmful on their own, but they can cause issues when not properly handled.

JavaScript Functions as Arguments


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function addition(x, y) {
		add = x + y;
		return add;
	}
	
	function second_level(a) {
		result = a * 2;
		document.write(result);
		document.write("<br><br>");
	}
	
	second_level(addition(30, 20));
	second_level(addition(50, 50));
	
</script>
	
</body>
</html>

That code calls a function addition that takes two arguments x and y, calculates their sum and returns the result.

Then, it defines a function second_level that takes one argument a, multiplies it by 2 and writes the result to the document.

Finally, it calls second_level twice, passing the result of calling addition with different arguments as the argument for second_level. This means that the result of calling addition is used as an argument to call second_level.

In other words, the result of addition(30, 20) is 50, which is passed as the argument a to the first call to second_level. The result of addition(50, 50) is 100, which is passed as the argument a to the second call to second_level


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function addition(x, y) {
		add = x + y;
		return add;
	}
	
	function second_level(a) {
		result = a * 2;
		return result;
	}
	
	function third_level(b) {
		result_b = b * 5;
		document.write(result_b);
		document.write("<br><br>");
	}
	
	//Addition = 7
	run_1 = addition(2, 5);
	
	//Multiplication by 2; 7 * 2 = 14
	run_2 = second_level(run_1);
	
	//Multiplication by 5; 14 * 5 = 70
	third_level(run_2);
	
</script>
	
</body>
</html>

This code calls three functions: addition, second_level, and third_level.

First, it calls addition with arguments 2 and 5, which returns 7. This result is assigned to the variable run_1.

Next, it calls second_level with run_1 (which is equal to 7) as the argument. second_level multiplies its argument by 2 and returns the result, which is 14. This result is assigned to the variable run_2.

Finally, it calls third_level with run_2 (which is equal to 14) as the argument. third_level multiplies its argument by 5, prints the result to the document, and adds a line break. In this case, the result is 70.

JavaScript Return Values

A return value is the value that a function or subroutine sends back to the caller. When a function completes its execution, it can optionally return a value, which is then used by the calling code. 

Return values are an important part of programming because they allow functions to be used in a wide variety of contexts, and they allow complex programs to be broken down into smaller, more manageable pieces.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function addition(x, y) {
		add = x + y;
		return add;
	}
	
	document.write(addition(100, 20));
	document.write("<br><br>");
	
	document.write(addition(25, 75));
	document.write("<br><br>");
	
	document.write(addition(5, 995));
	
</script>
	
</body>
</html>

This code defines a function named addition that takes two parameters: x and y.

The function adds the values of x and y together and stores the result in a variable named add. It then uses the return keyword to send the value of add back to the caller.

The code then calls the addition function three times with different arguments, using the document.write() method to write the return value of each call to the web page, separated by two line breaks (<br><br>).

When you run this code, you should see the return values of the addition function written to the web page, separated by two line breaks. The first call to addition should return 120, the second call should return 100, and the third call should return 1000.

JavaScript Multiple Parameters

Functions with multiple parameters are simply functions that take more than one input value. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function multi_param(x, y, z) {
		document.write(x);
		document.write("<br><br>");
		
		document.write(y);
		document.write("<br><br>");
		
		document.write(z);
		document.write("<br><br>");
	}
	
	multi_param(10, 50, 100);
	
</script>
	
</body>
</html>

This code defines a function named multi_param that takes three parameters: x, y, and z.

The function uses the document.write() method to write the values of each parameter to the web page, separated by two line breaks (<br><br>).

The code then calls the multi_param function once with the values 10, 50, and 100 as arguments.

When you run this code, you should see the values 10, 50, and 100 written to the web page, each separated by two line breaks. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function multiplication(x, y) {
		result = x * y;
		document.write("Result: " + result);
	}
	
	multiplication(100, 20);
	
</script>
	
</body>
</html>

This code defines a function named multiplication that takes two parameters: x and y.

The function multiplies the values of x and y together and stores the result in a variable named result.

It then uses the document.write() method to write a string to the web page that includes the word "Result" and the value of the result variable.

The code then calls the multiplication function once with the values 100 and 20 as arguments.

When you run this code, you should see the string "Result: 2000" written to the web page.

This code demonstrates how a function can perform some computation or action using its parameters and return a result to the caller.

JavaScript Function Parameters

Parameters are inputs to a function that allow the function to operate on different values or data sets each time it is called.

When defining a function, you can specify one or more parameters in the function declaration. These parameters act as placeholders for the actual values that will be passed into the function when it is called. 

You can have any number of parameters in a function declaration, separated by commas. When you call the function, you can pass in as many arguments as necessary to match the number of parameters in the function definition.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function with_param(x) {
		alert(x);
	}
	
	with_param(3423423);
	with_param("Something");
	
</script>
	
</body>
</html>

This code defines a function named with_param that takes a single parameter x.

The function displays an alert box with the value of the x parameter when it is called using the alert(x) statement.

The code then calls the with_param function twice with different arguments: 3423423 and "Something".

The first call passes in the number 3423423 as the value for the x parameter, while the second call passes in the string "Something".

When you run this code, you should see two alert boxes pop up in your browser window, each containing the value of the corresponding argument that was passed into the with_param function.

The first alert box should display the number 3423423, and the second alert box should display the string "Something"


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function with_param(x) {
		alert(x);
		document.write(x);
		document.write("<hr>");
		document.write(x);
	}
	
	with_param("Something New");
	
</script>
	
</body>
</html>

This code defines a function named with_param that takes a single parameter x.

The function displays an alert box with the value of the x parameter using the alert(x) statement.

It then uses the document.write() method to write the value of the x parameter to the web page twice, separated by a horizontal rule (<hr>).

The code calls the with_param function once with the string "Something New" as the argument.

When you run this code, you should see an alert box pop up in your browser window with the message "Something New".

Below the alert box, you should see the string "Something New" written twice to the web page, separated by a horizontal rule. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function with_param(x) {
		alert(x);
		document.write(x);
		document.write("<hr>");
		document.write(x);
	}
	
	function grab_param(z) {
		with_param(z);
	}
	
	grab_param("Func to Func");
	
</script>
	
</body>
</html>

This code defines two functions: with_param and grab_param.

The with_param function takes a single parameter x and displays an alert box with the value of the x parameter. It then writes the value of the x parameter to the web page twice, separated by a horizontal rule (<hr>).

The grab_param function takes a single parameter z. It calls the with_param function and passes in the z parameter as an argument.

The code then calls the grab_param function once with the string "Func to Func" as the argument.

When you run this code, you should see an alert box pop up in your browser window with the message "Func to Func".

Below the alert box, you should see the string "Func to Func" written twice to the web page, separated by a horizontal rule.

This code demonstrates how one function can call another function and pass in a parameter as an argument. In this example, the grab_param function calls the with_param function and passes in the z parameter as the value for the x parameter in the with_param function.

JavaScript Functions

A function is a block of code that performs a specific task and can be reused multiple times throughout a program. Functions allow you to write code once and use it many times, which can help reduce redundancy and make your code more efficient.

Here is the basic syntax for creating a function in JavaScript: 

function functionName(parameters) {
  // code to be executed
}
  • function: The keyword that tells JavaScript you are creating a function.
  • functionName: The name of the function. This can be any valid identifier.
  • parameters: The inputs to the function. These are optional, and you can have multiple parameters separated by commas.
  • code to be executed: The code that the function will execute when called.

Here is an example of a simple function that takes two parameters and returns their sum: 

function addNumbers(num1, num2) {
  return num1 + num2;
}

To call this function, you would use the function name followed by the arguments (values) you want to pass in: 

var result = addNumbers(2, 3);
console.log(result); // Output: 5

This would call the addNumbers function with arguments 2 and 3, and store the result in the result variable. The function would then return the sum of the two numbers, which would be 5.

Functions are a fundamental concept in JavaScript, and they are used extensively in web development to perform a variety of tasks, from simple calculations to complex data processing and manipulation. They are an essential part of the language and one of the reasons why JavaScript is so powerful and flexible. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function some_func() {
		alert("I am from Func");
	}
	
	function run_3_times() {
		some_func();
		some_func();
		some_func();
	}
	
	run_3_times();
	
</script>
	
</body>
</html>

This code defines two functions: some_func and run_3_times.

The some_func function simply displays an alert box with the message "I am from Func".

The run_3_times function calls the some_func function three times in a row using the some_func(); statement three times.

Finally, the run_3_times(); statement calls the run_3_times function, which in turn calls the some_func function three times.

When you run this code, you should see three alert boxes pop up in your browser window, each containing the message "I am from Func". 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function some_func() {
		alert("I am from Func");
	}
	
</script>

<input type="button" value="Click Me"
onclick="some_func()">
	
</body>
</html>

This code defines a function named some_func that displays an alert box with the message "I am from Func".

It also creates an HTML input button with the label "Click Me". When this button is clicked, the some_func function is called using the onclick event handler.

The onclick event handler is a built-in JavaScript event that is triggered when the button is clicked. In this case, it calls the some_func function using the some_func() statement.

When you load this code in a browser and click the "Click Me" button, you should see an alert box pop up with the message "I am from Func".

JavaScript Variable Name Rules

In JavaScript, variable names must adhere to the following rules:

  1. Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
  2. After the first character, variable names may also contain digits (0-9).
  3. Variable names are case sensitive. For example, myVariable and MyVariable are two distinct variable names.
  4. Variable names cannot be a reserved keyword. For example, var, function, and return are all reserved keywords and cannot be used as variable names.
  5. Variable names should be descriptive and meaningful, making it easy for other developers to understand what the variable is used for.

JavaScript Data Types

A data type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. It defines what kind of value a variable can hold, what operations can be performed on that value, and how the data is stored in memory.

Common data types in programming include integers, floating-point numbers, strings, boolean values, arrays, and objects, among others.

Understanding data types is important in programming as it helps ensure that the program operates as expected, and it can also impact performance and memory usage.


<!DOCTYPE html>
<html>
<head>
<script src="main.js"></script>
</head>
<body>

<script>
	var x = 234234;
	var y = 23423.23;
	var z = -234232.234523;
	var c = "A";
	var s = "klkjhljksdahf jkhas dlfkjh";
	
	document.write(x);
	document.write("<br>");
	
	document.write(y);
	document.write("<br>");
	
	document.write(z);
	document.write("<br>");
	
	document.write(c);	
	document.write("<br>");
	
	document.write(s);
	document.write("<br>");
	
</script>
</body>
</html>

This JavaScript code declares and initializes variables of different data types and then uses the document.write() method to display their values.

The variables and their values are:

  • x is an integer with the value of 234234.
  • y is a floating-point number with the value of 23423.23.
  • z is a negative floating-point number with the value of -234232.234523.
  • c is a string containing a single character A.
  • s is a string containing a sequence of characters klkjhljksdahf jkhas dlfkjh.

The code prints each variable's value on a new line by using the document.write() method and HTML line break tag <br>.

An integer is a whole number, meaning it doesn't have any decimal or fractional part. Examples of integers are 0, 1, -10, 100, etc.

A float, or a floating-point number, is a number that has a decimal point, allowing for fractional values. Examples of floats are 1.2, -3.14159, 6.66, etc.

A string is a sequence of characters, such as letters, numbers, and symbols, enclosed in quotation marks. Examples of strings are "hello world", "123", "John Smith", etc.

JavaScript Variables

In programming, a variable is a container that holds a value or a reference to a value. It can be thought of as a named placeholder or memory location where data can be stored and accessed.

The value stored in a variable can be changed or manipulated during the program execution, making it a powerful programming construct. Variables are used to store different types of data such as numbers, strings, objects, and more, and they are a fundamental concept in all programming languages. 


<!DOCTYPE html>

<html>
<head>
<script src="main.js"></script>
</head>

<body>

<script>

	var x = "Hack The Planet";
	var y = 1234;
	var z = "<hr>";
	
	document.write(x);
	document.write(y);
	document.write(z);
	
	document.write("Custom Stuff <br>");
	document.write("Something Else <br>");
	
</script>
	
</body>
</html>

This HTML code includes an external JavaScript file called "main.js" and defines a script block in the body section. Within the script block, three variables are defined: x, y, and z.

The first variable, x, is a string literal containing the text "Hack The Planet". The second variable, y, is an integer with the value of 1234. The third variable, z, is a string literal containing an HTML horizontal rule element.

The script block then uses the document.write method to output the values of the three variables to the HTML document. It first writes the value of x, then the value of y, and finally the value of z. It then writes the strings "Custom Stuff" and "Something Else" to the document, each on a new line.

JavaScript Comments

Comments in JavaScript are useful for adding human-readable explanations or annotations to your code, without affecting how the code is interpreted or executed by the computer. Comments are not executed as part of the program and are therefore ignored by the compiler or interpreter.

There are several reasons why comments are useful in JavaScript:

  1. Explanations: Comments can be used to explain the purpose of a piece of code or how it works. This can help other developers who are working on the same codebase to understand the logic behind the code, making it easier for them to maintain and modify the code.

  2. Debugging: Comments can also be used to temporarily disable a piece of code during debugging, without actually deleting the code. This makes it easy to isolate and identify the source of a bug.

  3. Documentation: Comments can also be used to automatically generate documentation for a codebase. Special tools can parse the comments in the code and use them to generate API documentation, user manuals, and other types of technical documentation.

Comments are an essential part of writing high-quality, maintainable code that is easy for others to understand and work with. 


<!DOCTYPE html>

<html>
<head>
<script src="main.js"></script>
</head>

<body>

<script>

	/*
	document.write("Hack The Planet");
	document.write("<br><br><br>");
	document.write("Mess with the best, die like the rest");
	*/
	
</script>

<script>
	
	document.write("Hack The Planet");
	//document.write("<br><br><br>");
	//document.write("Mess with the best, die like the rest");
	
</script>
	
</body>
</html>

Comments in JavaScript are used to add descriptive or explanatory text within the code that is not executed by the browser or JavaScript engine. This helps to make the code more readable and understandable for other developers who may be working on the same project.

There are two types of comments in JavaScript:

  1. Single-line comment: This is used to comment on a single line of code. It starts with // and everything after that on the same line is considered a comment.
  2. Multi-line comment: This is used to comment on multiple lines of code. It starts with /* and ends with */. Everything between these two characters is considered a comment.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...