Professional Documents
Culture Documents
aergser34t364
aergser34t364
A “do...while” loop works similarly to “for” and “while” loops, but it has a
different syntax.
let i = 1;
do {
console.log(i);
i++;
} while (i <= 10);
In this example:
1
2
3
4
5
6
7
8
9
10
for…in Loop
The “for...in” loop is used to iterate over the properties of an object (a data
structure that holds key-value pairs).
It's particularly handy when you want to go through all the keys or properties of
an object and perform an operation on each of them.
const person = {
name: "Alice",
age: 30,
city: "New York"
};
for (let key in person) {
console.log(key, person[key]);
}
In this example:
You have an object named “person” with the properties “name,” “age,” and “city”
The “for...in” loop iterates over the keys (in this case, "name," "age," and
"city") of the “person” object
Inside the loop, it logs both the property name (key) and its corresponding value
in the “person” object
The output of this code will be:
name Alice
age 30
city New York
The “for...in” loop is a powerful tool when you want to perform tasks like data
extraction or manipulation.
Functions
A function is a block of code that performs a particular action in your code. Some
common functions in JavaScript are:
alert() Function
This function displays a message in a pop-up dialog box in the browser. It's often
used for simple notifications, error messages, or getting the user's attention.
alert("Hello, world!");
When you call this function, it opens a small pop-up dialog box in the browser with
the message “Hello, world!” And a user can acknowledge this message by clicking an
"OK" button.
prompt() Function
This function displays a dialog box where the user can enter an input. The input is
returned as a string.
Here’s an example:
Later, the code uses the name to greet the user by displaying a message, such as
"Hello, [user's name]!"
confirm() Function
This function shows a confirmation dialog box with "OK" and "Cancel" buttons. It
returns “true” if the user clicks "OK" and “false” if they click "Cancel."
The user can click either the "OK" or "Cancel" button to make their choice.
The “confirm()” function then returns a boolean value (“true” or “false”) based on
the user's choice: “true” if they click "OK" and “false” if they click "Cancel."
console.log() Function
This function is used to output messages and data to the browser's console.
Sample code:
parseInt() Function
This function extracts and returns an integer from a string.
Sample code:
The extracted integer is then stored in the variable “integerNumber.” Which you can
use for various mathematical calculations.
parseFloat() Function
This function extracts and returns a floating-point number (a numeric value with
decimal places).
Sample code:
Strings
A string is a data type used to represent text.
toUpperCase() Method
This method converts all characters in a string to uppercase.
Example:
toLowerCase() Method
This method converts all characters in a string to lowercase
Here’s an example:
concat() Method
The “concat()” method is used to combine two or more strings and create a new
string that contains the merged text.
It does not modify the original strings. Instead, it returns a new string that
results from the combination of the original strings (called the concatenation).
We use the “concat()” method on “string1” and provide “string2” as an argument (an
input value within the parentheses). The method combines the two strings and
creates a new string, stored in the “concatenatedString” variable.
The program then outputs the end result to your console. In this case, that’s
“Hello, world!”
match() Method
The “match()” method is used to search a string for a specified pattern and return
the matches as an array (a data structure that holds a collection of values—like
matched substrings or patterns).
The “match()” method is extremely useful for tasks like data extraction or pattern
validation.
const text = "The quick brown fox jumps over the lazy dog";
const regex = /[A-Za-z]+/g;
const matches = text.match(regex);
console.log(matches);
In this example, we have a string named “text.”
Then, we use the “match()” method on the “text” string and provide a regular
expression as an argument.
This regular expression, “/[A-Za-z]+/g,” does two things:
It matches any letter from “A” to “Z,” regardless of whether it's uppercase or
lowercase
It executes a global search (indicated by “g” at the end of the regular
expression). This means the search doesn't stop after the first match is found.
Instead, it continues to search through the entire string and returns all matches.
After that, all the matches are stored in the “matches” variable.
The program then outputs these matches to your console. In this case, it will be an
array of all the words in the sentence "The quick brown fox jumps over the lazy
dog."
charAt() Method
The “charAt()” method is used to retrieve the character at a specified index
(position) within a string.
Here’s an example:
The result is the character "w" because "w" is at position 7 within the string.
replace() Method
The “replace()” method is used to search for a specified substring (a part within a
string) and replace it with another substring.
It specifies both the substring you want to search for and the substring you want
to replace it with.
The result is a new string (“newtext”) that contains the replaced text. Meaning the
output is, “Hello, there!”
substr() Method
The “substr()” method is used to extract a portion of a string, starting from a
specified index and extending for a specified number of characters.
It specifies the starting index from which you want to begin extracting characters
and the number of characters to extract.
(Note that the first character is always considered to be at index 0. And you start
counting from there on.)
Events
Events are actions that happen in the browser, such as a user clicking a button, a
webpage finishing loading, or an element on the page being hovered over with a
mouse.
onclick Event
The “onclick” event executes a function or script when an HTML element (such as a
button or a link) is clicked by a user.
When the HTML page loads, it displays a button with the text "Click me"
When a user clicks on the button, the “onclick” attribute specified in the HTML
tells the browser to call the “changeText” function
The “changeText” function is executed and selects the button element using its id
("myButton")
The “textContent” property of the button changes to "Clicked!"
As a result, when the button is clicked, its text changes from "Click me" to
"Clicked!"
onmouseover Event
The “onmouseover” event occurs when a user moves the mouse pointer over an HTML
element, such as an image, a button, or a hyperlink.
It also has an “onmouseover” attribute specified, which indicates that when the
user hovers the mouse pointer over the image, the "showMessage ()" function should
be executed.
This function displays an alert dialog with the message "Mouse over the image!"
The “onmouseover” event is useful for adding interactivity to your web pages. Such
as providing tooltips, changing the appearance of elements, or triggering actions
when the mouse moves over specific areas of the page.
onkeyup Event
The “onkeyup” is an event that occurs when a user releases a key on their keyboard
after pressing it.
Then, when the user types or releases a key in the input field, the “handleKeyUp()”
function is called.
The function retrieves the input value from the text field and logs it to the
console.
This event is commonly used in forms and text input fields to respond to a user’s
input in real time.
It’s helpful for tasks like auto-suggestions and character counting, as it allows
you to capture users’ keyboard inputs as they type.