In a programming language, we are directly speaking to a computer and provided instructions to execute. Some of those most communicative instructions include text called strings that also make providing instruction to users of your programs straightforward.
Strings
Strings can be defined as a data type that represents a string of sequential characters. It is commonly implemented in an Array data structure and declared by a variable that will be stored in your computer's memory.
// String Declation:
const message = "I love strings"
By Value
In javascript, a string is a primitive data type that is immutable and javascript passes all of these types by value.
// String Immutability:
let color = 'yellow'
color[0] = 'm' // the value will not change the color value
console.log(color) // yellow
String Manipulation
Here are a few ways to make use of strings
// String Concatenation: appending strings to another string
let food = "cakes"
// concatenating a string literal to a the *food* variable that contains a string
console.log("hot" + food) // hotcakes
// String Interpolation: evaluation of values inside of a string
let size = "tall"
let str = `This bridge is so ${size}` // This bridge is so tall
// Notice the backticks(``) and dollar sign($)
String Methods
// length: returns size of the string
let title = "developer"
console.log(title.length) // 9
// charAt: returns character at index
let str = "developer"
console.log(str.charAt(0)) // d
// toUpperCase: returns a string with upper-casing
let str = "developer"
console.log(title.toUpperCase()) // DEVELOPER
// slice: returns a copy of a string with stripped characters
// String.prototype.slice(start, end)
let str = "developer"
console.log(title.slice(0,3) // dev
Let's chat about Strings
This was a crash course that I hoped helped you to learn more about strings. If you enjoyed this post feel free to leave a comment about your thoughts and experiences working with strings.
Happy Coding,
Terry Threatt
Top comments (0)