JS 2, String array methods, If and Switch statements, let and var properties, HTML forms and js validation and setInterval()/setTimeout()

We can use index and array methods on strings:

//We can use the index but can't edit a string like an array
"string"[2]        //"r"
"string"[2] = "e"  //"string"

//We can also use more specific methods
"string".charAt(2)     //"r"
"string".charCodeAt    //114
chevron-rightslice(), substring(), concat() and split() methods on stringshashtag

We can't use splice() on strings, slice() doesn't edit the string, it creates a new one:

//slice(x,y), negative values start from the end of the string/array
let str = "Apple, Banana, Kiwi";
str.slice(7 , str.length)    //"Banana, Kiwi"
str.slice(6, -2);            //" Banana, Ki"
str.slice(5)                 //", Banana, Kiwi"

substring( [starting index, number of digits] ) can only be used on strings:

str.substring(0, 5)          //"Apple"

concat() can work on strings too:

let ora= "orange"
str.concat("and ", ora)    //"Apple, Banana, Kiwi and orange"

split() returns an array from a pattern separator:

//it checks for (" ") and creates an array element from it
//We can limit the number of array elements

let siamo= "are you ready"
siamo.split("", 5)         //['a', 'r', 'e', ' ', 'y']
siamo.split(" ", 5)        //['are', 'you', 'ready']
siamo.split("   ", 5)      //['are you ready']

But arrays and strings are not of the same type:

We can convert each other using split(), toString() and join():

We use the Typeof operator and Array.isArray() to check the type of data:

String data contains only 1 value and so is primitive, while Object contains more.

If and Switch

The IF statement executes a block of code if its condition is true:

For multiple conditions we can use logical operators:

We can also use switch and cases:

chevron-rightSwitch (if) statementhashtag

We use break to finish each case and default as the else{}.

If more cases share the same code.

Strings, numbers, and objects can have falsy values, which will be false in boolean:

Ternary operator is a simplified conditional operator:

We can use the nullish coalescing operator for null/undefined values:

Text filter javascript and DOM exercise

Check this code implementing regex, forms, and setInterval:

Let, Var and differences

In 2015 the let keyword for variables was introduced:

The different scope can make the var accidentally change its value:

Regex and string matching

We use Regular Expression Search Methods to search patterns in strings:

To search more specific patterns with regex we can use:

We use the test() method to true/false if a pattern is present on the string, while exec() to return a pattern index match and input:

Then we have the .search() and .match() :

About variables used as new regex patterns:

About stricter filters:

HTML forms and JS validation

We use forms to collect user input:

<Form> is a container for inputs/labels and the action attribute allows us to redirect the form after submission:

form with input labels

For the check status and value of radio/checkbox buttons, we use the attributes .checked and value:

chevron-rightRadio and checkbox buttons submithashtag

In a form we can put label and input in a different order:

In javascript, we check the check state/value of a single button or queryselect() many with the same attribute.

Checkbox/radio buttons form submit

We use select to create a dropdown list.

chevron-rightSelect in the DOM and JShashtag

The <select> in the HTML will be:

When the select gets used it triggers the "change" event, we also use a js function to update the CSS style.

To push JS content into HTML DOM we can use AppendChild() or innerHTML.

AppendChild() will record and update its content while innerHTML will reset it.

We can replace() an innerHTML element using regex.

Setinterval() and SetTimeout()

The setInterval() method calls a function at specified millisecond intervals, it returns an ID that can be used to stop it using clearInterval():

We setInterval() using the new Date() object (that updates itself) to create a clock:

setInterval() used with new Dat

The setTimeout() method calls a function once after a number of milliseconds:

To repeat a setTimeout() function :

new Date() with setTimeout()

Last updated