Javascript Course


List with useful JavaScript methods for String, and functions to work with strings in JS scripts.

str.concat(s1, s2, ..) - joins the string 'str' with the substrings 's1, s2, ..' and returns a new string..
let str ='Hello, ';
let str2 = str.concat('CoursesWeb', '.net');
document.write(str2);
str.includes(s2) - returns True if the substring s2 is in 'str', otherwise returns False.
- This method is case-sensitive (makes difference between uppercase and lowercase letters).
let str ='Life is Happiness, Be Happy.';
if(str.includes('Happ')) document.write('The string contains: Happ');
str.endsWith(s2) - returns True if the string 'str' ends with substring 's2', otherwise False. It is case-sensitive.
let str ='JavaScript string.';
if(str.endsWith('ing.')) document.write('The string ends with: ing.');
str.indexOf(s2) - returns the position of the first occurrence of the substring 's2' in 'str', or -1, if the substring is not found.
let str ='JS example string.';
var ix = str.indexOf('example');
if(ix !=-1) document.write('The word "example" starts from index: '+ ix);
str.match(RegExp) - Compares a regular expression and a string to see if they match. Returns an array with the matches or 'null'.
const email ='some_name@domain.net';

//is_em is an array like: ['email', 'name', 'domain'] or null
var is_em = email.match(/^([A-z0-9]+[A-z0-9._%-]*)@([A-z0-9_\-\.]+\.[A-z]{2,5})$/i);

//if email is a valid address writes the name
if(is_em){
 document.write('The name from the address: "'+email+'" is: '+ is_em[1]);
}
str.repeat(nr) - returns a string consisting of the copies of the string 'str' repeated the given 'nr' times.
let str ='abc';
let str2 = str.repeat(2);
document.write(str2); // abcabc
str.replace(s0, s2) - replaces in 'str' the matched substring 's0' with the value from 's2'.
let str ='Visit: https://marplo.net';
let str2 = str.replace('marplo.net', 'gamv.eu');
document.write(str2); // Visit: https://gamv.eu
str.search(RegExp) - executes the search for a match between a regular expression and a specified string. Returns the position of the match, or -1 if no match is found.
let str ='The Peace is peaceful';
var ix = str.search(/peace/i);
document.write(ix); // 4
str.slice(start, end) - pulls out a specified section of a string (between start index and the end index) and returns a new string, or -1.
let str ='Tutorials JS';
document.write(str.slice(0, 5)); //Tutor
str.split(separator, limit) - separates a string into an array of strings based on a separator sent as a parameter to the method. Limit is optional (an integer that specifies the number of splits).
let str ='How are you?';
var ar_str = str.split(' '); //['How', 'are', 'you?']
document.write(ar_str[1]); // are
str.startsWith(s2) - returns True if 'str' starts with 's2', otherwise False. Is case-sensitive.
let str ='Simple JavaScript string.';
if(str.startsWith('Simp')) document.write('The string starts with: Sim');
str.substr(start, length) - returns a portion of the string specified with a starting position and through the specified number of character (length).
var str ='JS Tutorials';
document.write(str.substr(3, 5)); // Tutor
str.substring(start, end) - extracts the characters from a string, between two specified indices (not including "end" itself), and returns the sub-string.
let str ='Hi everyone';
document.write(str.substring(3, 7)); // ever
str.toLowerCase() - returns the string to lowercase letters.
let str = 'I love Peace';
document.write(str.toLowerCase()); // i love peace
str.toUpperCase() - returns the string to all uppercase letters.
let str ='I love Peace';
document.write(str.toUpperCase()); //I LOVE PEACE
str.trim() - trims whitespace from the beginning and end of the string.
let str =' Hello to Me. ';
document.write('<pre>'+ str +' (without trim)</pre>');
document.write('<pre>'+ str.trim() +' (with trim)</pre>');

Aditional to this method are: trimStart() and trimEnd(). To trim whitespace from beginning or from the end.

str.toFixed(n) - formatting a number as a numeric string to a value rounded to 'n' decimals.
let nr =78.468;
document.write(nr.toFixed(2)); // 78.47

- You can find a list with the methods of the String object at MDN: String JavaScript.


Global functions for strings

escape(string) - converts most special characters to hexadecimal character codes (except: '@ * _ + - . /').
let str ='Be&Well @!';
document.write(escape(str)); // Be&Well @!
unescape(string) - performs the reverse operation of the escape() function.
let str ='Be&Well @! ';
document.write(unescape(str)); // Be&Well @!
encodeURIComponent(string) - encodes characters that separate different components of a URL (except letters, digits, and the following characters: - _ . ! ~ * ` ( ) ).
So you can safely encode a string for use as a single component. This is most commonly used for encoding a string for use as a query string value or parameter.
let str ='https://coursesweb.net/?x=abc';
document.write(encodeURIComponent(str)); // https://coursesweb.net/?x=abc
decodeURIComponent(string) - performs the reverse operation of the encodeURIComponent() function.
let str ='https://coursesweb.net/?x=abc';
document.write(decodeURIComponent(str)); // https://coursesweb.net/?x=abc

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Methods of the String object in JS

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)