Basics of JavaScript
JavaScript is a powerful, versatile language used for adding interactivity to websites. If you're just starting with web development, JavaScript is an essential tool in your toolkit. In this post, we’ll cover the basics of JavaScript, including how to add JavaScript to your HTML, basic syntax, variables, data types, functions, and event handling.
1. Adding JavaScript to Your HTML
You can add JavaScript directly into your HTML file. The simplest way is by using the <script>
tag.
alert()
function will display a pop-up message saying "Welcome to JavaScript!" as soon as the page loads. This method is great for testing simple scripts, but for larger projects, it’s better to link an external JavaScript file.2. Variables and Data Types
Variables store information that can be used later in the program. In JavaScript, you can declare variables using let
, const
, or var
.
Example: Declaring Variables
Explanation:
let
allows you to reassign the variable later, whileconst
is used for values that shouldn’t change.var
is an older way of declaring variables but is still used. Generally, uselet
andconst
in modern JavaScript.
Data Types
JavaScript supports several data types:
- String: Text data, e.g.,
"Hello"
- Number: Numeric data, e.g.,
10
- Boolean: True or false, e.g.,
true
- Array: A list of items, e.g.,
[1, 2, 3]
- Object: A collection of key-value pairs, e.g.,
{ name: "Alice", age: 25 }
3. Basic Operations
You can perform operations like addition, subtraction, and concatenation.
4. Functions
Functions allow you to define reusable code blocks.
Example: Basic Function
Explanation:
function
declares a function.- Functions can take parameters (like
name
in this example) and return values.
5. Conditional Statements
Conditions allow you to make decisions in your code.
Example: If-Else Statement
6.Looping Statement
JavaScript provides several types of looping statements, which allow you to execute code multiple times. Here are the main looping statements:
1. for Loopfor (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
7. Event Handling
JavaScript is often used to handle events like clicks or key presses.
Example: Button Click Event
showMessage()
function, which shows an alert.HTML Structure:
- There are two input fields for entering numbers and a button to trigger the addition.
- The result will be displayed in an
<h2>
element with the idresult
.
JavaScript Function
addNumbers()
:- The function retrieves the values from the input fields, converts them to numbers using
parseFloat()
, and checks if they are valid numbers. - If valid, it calculates the sum and displays the result in the
<h2>
element. - If invalid, it displays an error message asking for valid numbers.
Explanation:
HTML Structure:
- An input field to enter the maximum number.
- A button that, when clicked, calls the findPrimes() function to display prime numbers.
- A <p> element with the id result to show the output.
- isPrime(number): Checks if a number is prime by seeing if it has any divisors other than 1 and itself.
- findPrimes(): Retrieves the user's input, iterates through numbers from 2 up to the entered number, and calls isPrime(i) for each. If isPrime(i) returns true, the number is added to the primes array.
- Finally, the function displays the list of prime numbers in the <p id="result"> element.
Comments
Post a Comment