Web Development Boot Camp: Week 4

Youra Cho
3 min readApr 26, 2020

--

Dive into JavaScript: Object and String methods

I scream for being so happy!!!

JavaScript is the most powerful programming language in the history of the internet. That’s why I longed for studying JavaScript. I am so glad to gain a working knowledge and hands-on experience in this Bootcamp. I am looking forward to seeing what I can do after 16 weeks.

Object

The Object is one of JavaScript's data types. It is used to store various keyed collections and more complex entities. An object consists of key-value pairs inside curly braces {}. I made an example of objects shown in the driver’s license.

let driver = {
firstName: 'Steve',
lastName: 'Joe',
gender: 'male',
dlNum: 'F2834604',
expDate: 04/25/2025,
class: "c"
}
console.log(driver.firstName) ; // steve (dot notation)
console.log(driver['firstName']); // steve (bracket notation)
delete driver.gender // delete gender: 'male' (key-value pair)
'expDate' in driver // true (in operator)

String Methods

  • Accessing a character: str[index], charAt() method
'Bootcamp'.charAt(3);  // returns "t"'Bootcamp'[3];  // returns "t"
  • Concatenating strings: “+” operator, str1.concat(str2, str3, …)
let str1 = 'Hello';
let str2 = 'World';
console.log(str1 + ' ' + str2) // Hello World
console.log(str1.concat(' ', str2)) // Hello World
  • Getting an index of a string value: indexOf(), lastIndexOf()method

The indexOf() method returns the index of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found. The indexOf() method is case sensitive.

The lastIndexOf() method returns the index of the last occurrence of the specified value, searching backward from fromIndex.

str.indexOf(searchValue [, fromIndex])'Blue Whale'.indexOf('Whale', 0)  // returns  5
'Blue Whale'.indexOf('Whale', 7) // returns -1
'Blue Whale'.indexOf('blue') // returns -1 (case sensitive)
'Brave new world'.lastIndexOf('w') // returns 10
  • Determining whether a string value may be found

The includes() method determines whether one string may be found within another string, returning true or false as appropriate. (src: MDN)

'Blue Whale'.includes('Blue')   // true
'Blue Whale'.includes('blue') // false
let sentence = 'The quick brown fox jumps over the lazy dog.';
let word = 'fox';
let checkTheword = `The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence.`console.log(checkTheword) // The word "fox" is in the sentence.
  • Dividing a string into a set of substrings: str.split(separator)

The split() method divides a String into an ordered set of substrings, puts these substrings into an array, and returns the array. This method is useful to deal with data of CSV(Comma-separated values) type.

str.split([separator])const monthString = ‘Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec’;let months = monthString.split(',');console.log(months)
> ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let csvGeolocation = `Los Angeles,34°03′N,118°15′W
New York City,40°42′46″N,74°00′21″W
Paris,48°51′24″N,2°21′03″E`;
let lines = csvGeolocation.split('\n');
lines[0]
> "Los Angeles,34°03′N,118°15′W"
lines[0].split(',')
> ["Los Angeles", "34°03′N", "118°15′W"]
  • Extracting a part of a string: str.substring(), str.slice()

Both methods extract a section of a string and return it as a new string, without modifying the original string.

str.substring(indexStart[, indexEnd])const dob = '12242010';
const month = dob.substring(0, 2);
const date = dob.substring(2, 4);
const year = dob.substring(4);
console.log("Month: " + month, ", Date: " + date, ", Year: " + year)
> Month: 12 , Date: 24 , Year: 2010

console.log(month + "/" + date + "/" +year)
> 12/24/2010
str.slice(beginIndex[, endIndex])let str = 'The morning is upon us.'console.log(str.slice(0, 11)) // The morningconsole.log(str.slice(-3)) // us.

--

--

Youra Cho

Software Engineer enjoying a creamy latte, sumo citrus, and fresh air from redwoods.