Maison  >  Article  >  interface Web  >  Les essentiels de JavaScript : partie 2

Les essentiels de JavaScript : partie 2

Mary-Kate Olsen
Mary-Kate Olsenoriginal
2024-10-01 20:19:30286parcourir

JavaScript Essentials: Part 2

Précédemment dans JavaScript Essentials : Partie 1, nous avons commencé avec certains types de données javascript, notamment une chaîne, un nombre, un booléen et un objet. Dans cette partie, nous allons approfondir un peu et examiner :

  • Interpolation de chaînes et aperçu de ses méthodes

Interpolation de chaîne

Nous savons comment créer des chaînes et les imprimer. Cependant, comment utiliser une variable dans une chaîne pour créer une sorte de phrase ? C'est à cela que sert l'interpolation de chaînes.

Exemple

Considérons ces variables.

const name = "John Doe";
const dateOfBirth = "2000-12-25";
const profession = "Software Engineer";
const numberOfPets = 2;
const weightOfProteinInGrams = 12.5;
const hasAJob = true;

Je ne pense pas que nous soyons nouveaux avec l'extrait ci-dessus. Si vous n'êtes pas sûr, reportez-vous à JavaScript Essentials : Partie 1.

Dans les interpolations de chaînes, nous utilisons des backticks. Oui, les backticks utilisés pour créer une chaîne sont utilisés pour créer des interpolations de chaîne. Tout ce que nous voulons faire, comme mentionné précédemment, c'est ajouter des variables aux chaînes pour créer une phrase.

const sentence = `Full name: ${name}, date of birth: ${dateOfBirth}, profession: ${profession}`;
console.log(sentence);

Nous avons une variable de chaîne appelée phrase. Dans la phrase, nous attribuons une chaîne qui utilise les valeurs attribuées au nom et à la date de naissance.

Est-il évident comment l'interpolation des chaînes a été effectuée ? Cela devrait. Nous mettons ${name} où name est une variable (dont la valeur) que nous voulons ajouter à notre variable chaîne. L'exemple ci-dessus utilise name et dateOfBirth. Faites pour les autres.

Nous aurions pu obtenir le même résultat si nous avions utilisé correctement la concaténation de chaînes en utilisant l'opérateur plus aux endroits où cela est requis.

Exemple

Puisque nous créons une chaîne, nous pouvons choisir le délimiteur que nous préférons.

const name = "John Doe";
const dateOfBirth = "2000-12-25";
const profession = "Software Engineer";
const numberOfPets = 2;
const weightOfProteinInGrams = 12.5;
const hasAJob = true;

const sentence =
  "Full name: " +
  name +
  ", date of birth: " +
  dateOfBirth +
  ", profession: " +
  profession;

console.log(sentence);

Indexer une chaîne

Vous entendrez sûrement beaucoup parler d’indexation. Alors parlons-en un peu maintenant. L'indexation est la même chose que la création de sous-scripts si cela peut aider. En bref, cela signifie que vous pouvez rechercher ou récupérer un caractère dans une chaîne en utilisant sa position numérique (index, en comptant à partir de zéro). Nous en discuterons (indexation) lorsqu'il s'agira de tableaux (la même idée s'applique aux tableaux).

Considérez la chaîne "Doe", la première lettre est "D", la deuxième est "o" et la troisième est "e". En javascript, l'indexation commence à zéro, donc le premier caractère (élément positionné) est à l'index 0, pointant vers « D ». Le second sera 1, après l'index 0, qui pointera vers « o », puis l'index 2 pointera vers le dernier (troisième) élément, le caractère « e » en troisième position. En JavaScript, index = position de l'élément - 1.

Exemple

const someName = "Doe";

const firstChar = someName[0];
const secondChar = someName[1];
const thirdChar = someName[2];

console.log(
  `The characters in "${someName}" are: '${firstChar}', '${secondChar}' and '${thirdChar}'`
);
// The characters in "Doe" are: 'D', 'o' and 'e'

Propriétés et méthodes de chaîne

Une chaîne est un objet. Cela signifie qu'ils ont des méthodes et des propriétés. Une méthode est une action ou une fonction qui peut être exécutée sur l’objet chaîne. Cet objet chaîne a des propriétés. Pour l'instant, nous pouvons dire qu'une propriété est comme une variable attachée à cet objet d'intérêt qui conserve un état (des données) sur ce même objet.

Nous examinons un nouvel opérateur appelé l'opérateur dot. Il est utilisé pour accéder aux propriétés et méthodes d'un objet.

Nous examinerons les propriétés et méthodes fréquemment utilisées. Je désignerai les propriétés par p et les méthodes par m.

  • length (p): is a property that returns the number of characters in a string. Hence the length of the string.
  • toUpperCase() (m): is a method that returns the string in uppercase
  • toLowerCase() (m): is a method that returns the string in lowercase
  • endsWith(key: string) (m): is a method that returns true when the string ends with the key parameter (a string) else false
  • startsWith(key: string) (m): is a method that returns true when the string starts with the key parameter (a string) else false
  • includes(key: string) (m): is a method that returns true when the string contains the key string else false
  • replace(oldStr: string, withNewString: string) (m): is a method that replaces oldStr in the string with withNewString and returns the newly updated string
  • trim() (m): is a method that removes starting or ending white spaces of a string and returns a new value
  • split(key: string) (m): returns an array of the string where key is used as a divider (to split the string at the point)
  • substring(startingIndex: number, optionalEndingIndex?: number) (m): returns a position of a string specified by the _startingIndex_ to the character at the optionalEndingIndex, when provided.
  • indexOf(key: string, optionalKeySearchingStartingAtIndex?: number) (m): returns the position of the first occurrence of key. When optionalKeySearchingStartingAtIndex is provided, the searching starts there. If there key is not found, it returns -1
  • charAt(index: number) (m): returns the character at index or an empty string

Example
This is a whole single snippet in a single file, string_methods.js

// Using string methods and properties
const fullName = "John Doe";
  • Length
// NB: whatever method we can call on a string literal,
//we can do the same for a variable

// find and print out the length of the fullName string
// variable using the length property
const fullNameLength = fullName.length;

// using string interpolations here
console.log(`The name, "${fullName}", has ${fullNameLength} characters`);
  • To uppercase and lowercase
// counting includes the spaces and symbols too
// update the value of fullName and see what happens

// this is the same as above but I will encourage you
// to stick to string interpolations in cases like this
// console.log("The name, " + fullName + ", has " + fullNameLength + " characters");

// convert uppercase and lowercase
console.log(`Upper: ${fullName.toUpperCase()}`);
console.log(`Lower: ${fullName.toLowerCase()}`);
  • Starts and ends with
// check if fullName starts with John
const startsWithKey = "John";
const fullNameStartsWithJohn = fullName.startsWith(startsWithKey);
console.log(
  `It is ${fullNameStartsWithJohn} that "${fullName}" starts with "${startsWithKey}"`
);

const endsWithKey = "Doe";
const fullNameEndsWithDoe = fullName.endsWith(endsWithKey);
console.log(
  `It is ${fullNameEndsWithDoe} that "${fullName}" ends with "${endsWithKey}"`
);
  • includes
// try this yourself, check if fullName starts with john and doe,
// then uppercase and lowercase the key and try again (for both cases)

// check if fullName as a space
const fullNameHasASpace = fullName.includes(" ");
// here we passed the key/search string directly without creating a variable.
// I want you to know that we can also do that
console.log(`It is ${fullNameHasASpace} that "${fullName}" has a space`);
// we could have also done
// console.log(`It is ${fullName.includes(" ")} that "${fullName}" has a space`);
  • replace
// try checking if the fullName contains the letter J, either uppercase or lowercase
// how would you achieve this? refer to the methods above for clues, first

// replace Doe with Moe
const moedFullName = fullName.replace("Doe", "Moe");
console.log(`New full name: ${moedFullName}`);

// try replacing spaces with underscore
const stringWithStaringAndTrailingSpaces = " John Doe ";
console.log(
  stringWithStaringAndTrailingSpaces
    .replace(" ", "JS") // replaces the first " "
    .replace(" ", "_") // replaces the second " "
    .replace(" ", "TS") // replaces the third " "
);

// it replaces only one instance at a time. so it was chained.
// I don't have to create a temp variable

// trust me, having your users start their email with spaces is annoying
// and sometimes it's the input keyboard (I get it, I am just venting off)
// but then again it will not harm trimming the unnecessary (white) spaces
// we will trim the whites off the stringWithStaringAndTrailingSpaces
// we'd use another method to let you know that the trimming worked
console.log(
  `Length of original string: ${stringWithStaringAndTrailingSpaces.length}`
);
  • trim
console.log(
  `Length of trimmed string: ${
    stringWithStaringAndTrailingSpaces.trim().length
  }`
);
// remember the definition of the length, it counts the spaces too
  • split
// split is really useful, we can split the full name into the first
// and last name in order as is
// i don't think it will be pretty to have spaces with the names
// we'd do the splitting at the space
console.log(stringWithStaringAndTrailingSpaces.split(" "));
console.log(stringWithStaringAndTrailingSpaces.trim().split(" "));
// printing the split string (becomes an array) shows the content
// of the array (or string to be precise)
// note any difference? Would the assumption still hold that the
// first element (object) in the array will be the first name and
// second (which should be the last actually) is the last name?
// this doesn't hold anymore because of the starting and trailing spaces
  • substring
// substring(starting [, ending])
const someLongString = "This is some long string";
const substringFromTwoToEight = someLongString.substring(2, 8);
console.log(substringFromTwoToEight);
// is is

const substringFromStartToTwelve = someLongString.substring(0, 12);
console.log(substringFromStartToTwelve);
// This is some

const substringFromTenth = someLongString.substring(10);
console.log(substringFromTenth);
// me long string
  • indexOf
// chartAt and indexOf
const veryWeakPassword = "qwerty12";

const indexOfTee = veryWeakPassword.indexOf("t");
console.log(`The first 't' in "${veryWeakPassword}" is at index ${indexOfTee}`);
// The first 't' in "qwerty12" is at index 4

// Note that there is no 'v'
const indexOfVee = veryWeakPassword.indexOf("v");
console.log(`The first 'v' in "${veryWeakPassword}" is at index ${indexOfVee}`);
// The first 'v' in "qwerty12" is at index -1
  • chartAt
// based on the result of the above (using the indexOf Tee which was 4)
// let's find the value at index 4
const characterAtIndexFour = veryWeakPassword.charAt(4);
// we could have passed 'indexOfTee'
console.log(`The character at index '4' is '${characterAtIndexFour}'`);
// The character at index '4' is 't'

const characterAtIndexNegativeOne = veryWeakPassword.charAt(-1);
console.log(`The character at index '4' is '${characterAtIndexNegativeOne}'`);
// returns an empty string
// The character at index '4' is ''

Password and email validation

We have discussed a lot of concepts about strings here. There are a lot more. Practically, these are some of the (biasedly) frequently used methods. Let's create a script for password and email validation.

Password rules
Password must:

  • be six characters
  • start with uppercase p, 'P'
  • end with an underscore
  • have uppercase q, 'Q'
  • have lowercase r, 'r'
  • have its fifth character as uppercase v, 'V'

Email rules

Email must:

  • be at least sixteen characters
  • be in all lowercase
  • not have 'email' in it
  • not include an underscore
  • have one '@'
  • have one '.'
  • end with '.com'
  • have the character before the '@' to be the character, uppercase v, 'V'

It is simple actually. So let's do password validation together and you'd do the email verification.

Password validation

// password_verification.js
const veryWeakPassword = "qwerty12";
console.log(`Password validation for "${veryWeakPassword}"`);

// - be six characters
const passwordLength = veryWeakPassword.length;
console.log(
  `- Password must have 6 characters => "${veryWeakPassword}" has '${passwordLength}' characters`
);
// so it is a valid password based on our rules?

// - start with uppercase p, 'P'
const startsWithPee = veryWeakPassword.startsWith("P");
console.log(
  `- Password must start with 'P' => it is ${startsWithPee} that "${veryWeakPassword}" starts with 'P'`
);

// we can also check the first character, index 0.
const firstCharacter = veryWeakPassword[0];
console.log(
  `- Password must start with 'P' => "${veryWeakPassword}" starts with '${firstCharacter}'`
);

// - end with underscore
const endsWithUnderscore = veryWeakPassword.endsWith("_");
console.log(
  `- Password must end with '_' => it is ${endsWithUnderscore} that "${veryWeakPassword}" ends with '_'`
);

// from the index concept, the last character will be at index, length of string minus one
const lastCharacter = veryWeakPassword[veryWeakPassword.length - 1];
console.log(
  `- Password must start with 'P' => "${veryWeakPassword}" ends with '${lastCharacter}'`
);

// - have uppercase q, 'Q'
const hasUppercaseQue = veryWeakPassword.includes("Q");
console.log(
  `- Password must have uppercase q, 'Q' => it is ${hasUppercaseQue} that "${veryWeakPassword}" has 'Q'`
);

// we can use the index approach
const indexOfUppercaseQue = veryWeakPassword.indexOf("Q");
console.log(
  `- Password must have uppercase q, 'Q' => 'Q' is at index '${indexOfUppercaseQue}' of "${veryWeakPassword}"`
);
// we know that index -1 means, there 'Q' was not found

// - have lowercase r, 'r'
const hasLowercaseArr = veryWeakPassword.includes("r");
console.log(
  `- Password must have lowercase r, 'r' => it is ${hasLowercaseArr} that "${veryWeakPassword}" has 'r'`
);

// we can use the index approach too
const indexOfLowercaseArr = veryWeakPassword.indexOf("r");
console.log(
  `- Password must have lowercase r, 'r' => 'r' is at index '${indexOfLowercaseArr}' of "${veryWeakPassword}"`
);
// we know that index -1 means, there 'r' was not found

// - have its fifth character as uppercase v, 'V'
// fifth character with have index = fifth position - 1 = 4
// const fifthCharacter = veryWeakPassword[4]
const fifthCharacter = veryWeakPassword.charAt(4);
console.log(
  `- Password must have its fifth character as uppercase v, 'V' => "${veryWeakPassword}" has its 5th character as '${fifthCharacter}'`
);

// using the index approach, 'V' must have an index of 4 (same index logic as above)
const indexOfVee = veryWeakPassword.indexOf("V");
console.log(
  `- Password must have its fifth character as uppercase v, 'V' => 'V' is at index '${indexOfVee}' of "${veryWeakPassword}"`
);

Output from the console.log

Password validation for "qwerty12"
- Password must have 6 characters => "qwerty12" has '8' characters
- Password must start with 'P' => it is false that "qwerty12" starts with 'P'
- Password must start with 'P' => "qwerty12" starts with 'q'
- Password must end with '_' => it is false that "qwerty12" ends with '_'
- Password must start with 'P' => "qwerty12" ends with '2'
- Password must have uppercase q, 'Q' => it is false that "qwerty12" has 'Q'
- Password must have uppercase q, 'Q' => 'Q' is at index '-1' of "qwerty12"
- Password must have lowercase r, 'r' => it is true that "qwerty12" has 'r'
- Password must have lowercase r, 'r' => 'r' is at index '3' of "qwerty12"
- Password must have its fifth character as uppercase v, 'V' => "qwerty12" has its 5th character as 't'
- Password must have its fifth character as uppercase v, 'V' => 'V' is at index '-1' of "qwerty12"

Conclusion

String forms part of almost any data structure one would use. So knowing how to manipulate them gives you the upper hand.

Try your hands on the email verification and don't hesitate to share your implementation.

We have more on javascript to discuss such as:

  • Object
  • Arrays
  • Control structures (if statements, loops)
  • Functions
  • Callbacks, promises, async & await
  • Next big thing

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn