- 3 years ago
- Zaid Bin Khalid
- 2501 Views
-
4
In this session, you will learn how to handle events and manipulate strings in JavaScript with the help of an example.
Understanding Events and Event Handlers
When users interact with the web page, an event that happens, like when you check or click a button or link, enters text messages into an input box, presses a key on the keyboard, moves the mouse pointer, and submits a form, etc.
There are many other ways to assign an event handler. The easiest way is to add them directly to the start tag by using the single event handler. As shown in the example:
<button type="button" onclick="alert('Hello World!')">Click Me</button>
Events categorized
In general, events can be categorized into four main groups.
- Mouse events.
- Keyboard events.
- Form events.
- Document/window events.
What is String in JavaScript
A string is a combination of all sequences of letters, numbers, special characters, and arithmetic values. As shown in the example below:
var myString = 'Hello World!'; // Single quoted string
var myString = "Hello World!"; // Double quoted string
JavaScript Escape Sequences
Where you want to use characters that cannot be typed by using a keyboard, Escape sequences are used.
Commonly used escape sequences:
Here are some other most commonly used escape sequences.
- Replaced by the newline character \n
- Replaced by the tab character \t
- Replaced by the carriage-return character \r
- Replaced by the backspace character \b
- Replaced by a single backslash \\ (\)
As shown in the example, how escape sequences work:
var str1 = "The quick brown fox \n jumps over the lazy dog.";
document.write("<pre>" + str1 + "</pre>"); // Create line break
var str2 = "C:\Users\Downloads";
document.write(str2); // Prints C:UsersDownloads
var str3 = "C:\\Users\\Downloads";
document.write(str3); // Prints C:\Users\Downloads
Performing Operations on Strings
There are several properties and methods to operate the operation on string values by using JavaScript. By creating a temporary wrapper object for primitive data types JavaScript making it possible. This process works automatically by the JavaScript interpreter in the background.
Getting the Length of a String
The length property returns the length of the string. It depends on the number contained in the string. Distinctive characters of number include as well, such as \t or \n
var str1 = "This is a paragraph of text.";
document.write(str1.length); // Prints 28
var str2 = "This is a \n paragraph of text.";
document.write(str2.length); // Prints 30, because \n is only one character
Finding a String inside another String
By using the indexOf ( ) method; to find a substring or string within another string. This type of method will return the index or position of the first specified string within a string.
var str = "If the facts don't fit the theory, change the facts.";
var pos = str.indexOf("facts");
alert(pos); // 0utputs: 7
- 3 years ago
- Zaid Bin Khalid
- 2501 Views
-
4