Vue js charCodeAt Method:The UTF-16 code unit at the specified index is represented by an integer between 0 and 65535 by the charCodeAt() method. The index is less than the length of the string, greater than 0, but less than 1. It gives back a number that represents the character’s UTF-16 code unit value at the specified index. If index is outside of the allowed range, charCodeAt() returns NaN. String.charCodeAt returns the ASCII code of the character at the specified index. To extract specific characters from a string, use the charat() method in Vue, as shown below.
How to Use in Vue Js charCodeAt() method?
In Vue.js, you can use the string.prototype.charCodeAt(“3”) native javascript method to convert specific characters into an ASCII code, as shown simply below:
Vue Js charCodeAt() function | Example
<div id="app">
<button @click='myFunction'>click me </button>
<p>{{string}}</p>
<p>String Char: {{results}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data()
{
return{
string: 'Fontawesomeicons',
results : ''
}
},
methods:{
myFunction(){
this.results = this.string.charCodeAt(4);
},
}
}).mount('#app')
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/9-3.webp)
Enter a character and Get ASCII Code in Vue Js
For example, if you enter the letter F in input, you will get the Ascii code 70 by using native JavaScript in Vue.js.
Vue Js get Ascii code of String character
<div id="app">
<input type="text" @input="myFunction" maxlength ='1' v-model="character"/>
<p>Character ASCII code: {{results}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data()
{
return{
results : '',
character:''
}
},
methods:{
myFunction(){
this.results = this.character.charCodeAt();
},
}
}).mount('#app')
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/10-3.webp)
How to Get ASCII code of last character in string using Vue Js?
In Vue.js, get the ASCII code of the last string element. Start by using the str.length function to determine the total number of characters in the given string. Because indexing starts at 0, use string.charCodeAt(string.length-1) to get the ASCII code of the string’s last character.
Vue JS Get the Unicode of the last character in a string
<div id="app">
<button @click="myFunction">click me </button>
<p>{{string}}</p>
<p>Character ASCII code: {{results}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data()
{
return{
results : '',
string:'FONTAWESOMEICONS'
}
},
methods:{
myFunction(){
this.results = this.string.charCodeAt(this.string.length-1);
},
}
}).mount('#app')
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/11-3.webp)