JavaScript charCodeAt

JavaScript charCodeAt

JavaScript charCodeAt method, to be used on strings, takes an index as an optional parameter. This method returns the Unicode character of the corresponding character specified by the index.

So the syntax is as follows: string.charCodeAt(index).

If the index is not specified, it will have a default value of 0.

Recall that Unicode is an encoding that assigns a unique number to each alphanumeric character. This encoding is independent of the language used.

For more information, see the following link: unicode encoding.

JavaScript charCodeAt

In this first example we will get the Unicode of the first character of a string.

So here’s a possible implementation of the JavaScript charCodeAt method:



var sentence = 'do coding creativo';
var character = sentence.charCodeAt(0);
console.log(character);

So, in the browser console, we get the value 102, which is the corresponding Unicode character of the letter f.

Using the JavaScript charCodeAt method in the last character of a string

In this example we get the Unicode encoding of the last character of a string taken as input.

To do this, it is necessary to use the length property on the string which, remember, returns the number of characters of a given string.

Here is the complete example:



var sentence = 'do coding creativo';
var character = sentence.charCodeAt(sentence.length-1);
console.log(character);

In this case, the value 101 is obtained, which corresponds to the Unicode encoding of the letter o.

If we try to obtain the Unicode encoding of an index that does not exist then we will have the value NaN (Not a Number).

Conclusion

In this lesson we have seen some practical examples on using the JavaScript charCodeAt method, in the next lessons we will see many other examples with other methods on strings and arrays.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

JavaScript charAt

JavaScript charAt

The JavaScript charAt method used on strings returns one character from a string. The position of the character is indicated in the index between round brackets.

So for example charAt (0) returns the first character, while charAt (1) returns the second and so on.

The syntax is therefore the following: string.charAt(index)

Therefore the method accepts a mandatory parameter, index, which represents the position of the character to search for in a string.

JavaScript charAt – examples

In this first example we will search for the first character of a string in a simple sentence.

Here is a possible implementation:



var sentence = 'do coding creativo';
var character = sentence.charAt(0);
console.log(character);

In this case, the result is the letter f (the first letter of the sentence).

If, on the other hand, we look for a position in the sentence that does not exist, we will not get any value, as in the example below:



var sentence = 'do coding creativo';
var character = sentence.charAt(200);
console.log(character);

Last character of a string using the JavaScript charAt method

If we want to determine the last character of a string, then we can use the length property on the string. In fact, remember that str.length returns the amount of characters in a string, so to get the last character, since the index starts from 0, we must subtract 1.

Here is the complete example:



var sentence = 'do coding creativo';
var character = sentence.charAt(sentence.length-1);
console.log(character);

Capitalize the first character with the JavaScript charAt and uppercase method

In this exercise we extract the first character, convert it to uppercase with the toUpperCase method and then concatenate everything else using the substr method.

Here is the complete example:



var sentence = 'do coding creativo';
var sentenceUpper = sentence.charAt(0).toUpperCase() + sentence.substr(1);
console.log(sentenceUpper);

In this short lesson we have implemented some very simple exercises using the JavaScript charAt method, continuing in the tutorial you will find many other application examples.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

JavaScript search

JavaScript search

JavaScript search method looks for a string and returns the location of its match.

The value to search for can be a string or a regular expression. If the occurrence is found, the position of the string is returned, otherwise the value -1 is returned.

The syntax is therefore as follows: string.search (valueToSearch).

Therefore, the method accepts a mandatory parameter, valueToFind, which represents the value to search for in the string.

JavaScript search – examples

In this first example we will search for the occurrence of a string in a simple sentence.

Here is a possible implementation:



var sentence = 'making coding creativo at school';
var sentenceSearch = sentence.search('school');
console.log(sentenceSearch);

In this way, you will get the position of the word school which corresponds to position 27.

If, on the other hand, we look for a word that does not exist in the sentence, we will get the value -1, as in the example below:



var sentence = 'making coding creativo at school';
var sentenceSearch = sentence.search('school');
console.log(sentenceSearch);

JavaScript search – regular expressions

JavaScript search method is case sensitive! So if for example we try to search for School with a capital S, we will get the return value -1, indicating that the string was not found.

To obtain a case insensitive search we must use a regular expression and in particular we will need the modifier i which performs the correspondence without distinction between upper and lower case, as in the example below:



var sentence = 'making coding creativo at school';
var sentenceSearch = sentence.search(/School/i);
console.log(sentenceSearch);

As we can see, we used slashes instead of quotes.

Conclusion

In this lesson we have implemented some very simple examples of using the search method in JavaScript and we have also mentioned regular expressions. We’ll get back to talking about regular expressions further on in our JavaScript tutorial.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

JavaScript replace

JavaScript replace

The JavaScript replace method is used on strings to replace one or more values.

The syntax is therefore the following: string.replace (value, newValue).

Then the method accepts two mandatory parameters, the first value representing the value to search for in the string and which will subsequently be replaced by the second parameter, newValue.

JavaScript replace – examples

In this first example we will replace one word with another.

Here is a possible implementation:



var sentence = 'coding since high school';
var sentenceChange = sentence.replace('high','primary');
console.log(sentenceChange);

In this way, if there are more occurrences, only the first one it encounters will be replaced. If we want to replace them all we can use the global operators.

JavaScript replace and Global operator g

With the replace method of JavaScript you can make sure that if there are more occurrences, they are all replaced using the global operator g, so we don’t stop at the first one.

This operator is used in conjunction with regular expressions which have the following syntax / pattern / modifiers. Then we enclose the value to be replaced in / and then add the modifier, in our case g.

Attention g is case-sensitive, so using this modifier does not substitute words that are not case-sensitive.

Here is an example of using the global operator g with the replace method of JavaScript.



var sentence = 'Coding since high school. In first grade high school and second grade High school ';

var sentenceChange = sentence.replace(/high/g,'primary');
console.log(sentenceChange);

In this case you will get this result: Coding since primary school. In first grade high school and second grade primary school.

So it did not replace Superiore as it has a capital letter.

JavaScript replace and Global operator gi

You can use the global operator g with i, where i is a modifier as it modifies the search so that it is not case sensitive.

The global operator gi is therefore case-insensitive, so used with the replace method of JavaScript it allows you to replace all occurrences regardless of whether they are uppercase or lowercase.

So here’s an example:



var sentence = 'Coding since high school. In first grade HIGH school and second grade high school ';

var sentenceChange = sentence.replace(/high/gi,'primary');
console.log(sentenceChange);

In this example, all the terms ‘High’ will be replaced, regardless of how they are written.

In this lesson we have seen some very simple examples of using replace in JavaScript and we have mentioned regular expressions. We will come back to talking about regular expressions further on in the JavaScript tutorial.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

JavaScript lastIndexOf

JavaScript lastIndexOf

The JavaScript lastIndexOf method can be used, as well as indexOf, with both arrays and strings. In this lesson we will study it by applying it to strings.

The syntax is therefore the following: string.lastIndexOf (value, start).

The return value is a numeric value that represents the position of the last element’s position in the string, if there is more than one. If it doesn’t find this value, it returns -1.

As with indexOf, the value, i.e. the first argument passed as a parameter, can be a word, a number or a phrase. If more than one occurrence of this value is found, the method always returns the index of the last value found.

Instead the second parameter, beginning, is the starting point to search for the value. In some cases it may be necessary not to start from the beginning of the string. If start is not specified, it starts from position 0.

JavaScript lastIndexOf – examples

We use this method in a first example, where we take a string and search in it for the last occurrence of a word.

Here is a simple example:



var coding = 'Coding is fun and creative. Try coding yourself.';

var indexString = coding.lastIndexOf('coding');
console.log(indexString);

In the browser console we will see as a result 60, that is the position of the last word. If we had used indeOf we would have obtained 5, which is the position of the first word.

If the element to be searched for is not present then the JavaScript lastIndexOf method returns the value -1, so we modify the value to search for in the string, as in the example below:



var coding = 'Coding is fun and creative. Try coding yourself.';

var indexString = coding.lastIndexOf('the coding');
console.log(indexString);

In the browser console we will therefore display the value -1.

In this lesson we studied the use of the JavaScript lastIndexOf method with strings, remember that the same method can also be applied to arrays.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

JavaScript indexOf

JavaScript indexOf

The JavaScript indexOf method can be used with both arrays and strings. indexOf in this lesson is applied to strings.

The syntax of this JavaScript method is therefore the following: string.indexOf (value, start).

Returns a numeric value that represents the position of the element in the string. If it doesn’t find the value, it returns -1.

In particular, the string represents where to look for the value. Where the value, that is the first argument passed as a parameter, can be a word, a number or a sentence. If more than one occurrence of this value is found, the method still returns the index of the first value found.

The second parameter, beginning, represents the starting point to carry out the search, as in some cases it may be necessary not to start from the beginning. If not specified, it starts from 0.

JavaScript indexOf – examples

Let’s take a simple example of using this method.

We create a sentence and through the indexOf method we search for the word thought, for example.



var coding = 'Programming helps the development of logical thinking, let's have fun learning JavaScript';

var indexString = coding.indexOf('thought');
console.log(indexString);

As a result in the console we will find the value 34 which indicates the position of the word thought.

For example, if the sentence were to contain the word thought several times, it would always give position 34.

We said that if it does not find the element, the indexOf method in JavaScript returns the value -1, so let’s try to change the value to search for in the string, as in the example below:



var coding = 'Programming helps the development of logical thinking, let's have fun learning JavaScript';

var indexString = coding.indexOf('thought');
console.log(indexString);

So in this case in the console we will see as a result -1 as the value passed as argument is not contained in the string.

In this lesson we studied the use of the JavaScript indexOf method with strings, the same method can be applied to arrays, as we will see in this lesson: indexOf with arrays.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants