Thursday, 24 October 2019

JS : Strings

Strings


When you read lessons in this course, you can highlight a single line and save it to your notes. Simply highlight a single line with your mouse (or your finger on mobile) and then a popup will appear asking you to save it to your notes.
You will be able to find these notes in the challenge page by clicking on the Note icon on the top bar, next to the logo. Try it in the next page!
Popular notes will also be shown in yellow highlights which can be easily added to your notes with a single click. And you can also disable those popular highlights from the top right menu.

You can create a string in JavaScript by simply using the double quotes (") or single quotes (').
Here's an example:
"This is a string"; 'this is another string!'
There is no difference between using a double quote or a single quote. They are exactly the same. Both of these strings do not support interpolation (which means interpolating a variable inside of them). String interpolation will be covered in a future lesson.

Basic String properties

  • the .length property is used to return the length of the string.
Here's an example of getting the length of "Nice!":
"Nice!".length; //5
We will learn about variables later in this course, but assuming you have a variable called description, here's how you'd get its length:
description.length;

Basic String methods

Here are some common methods that you can call on strings:

.includes(searchString)

This method returns true when the searchString is included inside the parent string. For example:
"Hello World".includes("World"); // true "Hello World".includes("Potato"); // false

.toUpperCase()

This will return a new string that has all of its characters in upper case:
"hello".toUpperCase(); // "HELLO";

.toLowerCase()

This will return a new string that has all of its characters in lower case:
"NICe".toLowerCase(); // "nice";
Hint: Stuck? Feel free to use CodeToGo to search for common use cases

A note on tests & sample usage

On the left of the screen, you've got tests running your code. And in your editor, you will almost always have some sample usage code.
function sum(a, b) {
    return a + b;
}

//sample usage
sum(1, 3);
The sample usage code is meant to illustrate how your code will be used. However keep in mind that we will take the function that you wrote, and call it against several other possibilities to make sure that you've got the correct answer.
Feel free to use console.log() for the parameters of the function to see what kind of tests we're running.
So even though the sample usage is sum(1, 3), we run more tests in the background with several values. We also often run it with edge case values such as sum(0, 0). All of this simulates a real Test Driven Development environment.

A note on console.log

When solving challenges feel free to add:
console.log(variable_or_expression)
to see its result in the console on the bottom left.



You can also use it to better understand the tests that you're trying to pass.

email mailto: pretext

 <a href="mailto:designoutsourced.com+info@gmail.com?subject=Maklumat%20lanjut%20pakej&body=Hai,%20saya%20berminat%20tahu%20lebi...