Detailed explanation of JavaScript's built-in objects Math and strings

Detailed explanation of JavaScript's built-in objects Math and strings

Math Objects

  • Math is a tool class object that encapsulates properties and methods related to mathematical operations.

Common properties

  • Math.PI: represents pi

Common methods

  • Math.abs(x) : Returns the absolute value of x
  • Math.pow(x,y) : Returns x raised to the power of y
  • Math.sqrt(x) : Returns the square root of x
  • Math.round(x) : Returns x rounded to the nearest integer
  • Math.floor(x) : Returns the largest integer less than or equal to x
  • Math.ceil(x) : This function returns the smallest integer greater than or equal to x.
  • Math.max() : Returns the maximum value among the arguments
  • Math.min() : Returns the minimum value among its arguments

Math.random()

  • Returns a pseudo-random number (between 0.0 and 1.0) (excluding 1)
  • Generate a random number in [0,x]
    • Math.round(Math.random()*x)
    • [0,10]: Math.round(Math.random()*10)
  • Generate a random number [x,y]
    • Math.round(Math.random()*(yx)+x
    • Random number from [1,10]: Math.round(Math.random()*9)+1

String Methods

The length property

  • Can be used to get the length of a string
var str = "Hello World!!!";
var result = str.length;
console.log(result);

insert image description here

charAt()

  • Can return the character at a specified position in a string
  • Get the specified character by index
var str = "Hello World!!!";
var result = str.charAt(6);
console.log(result);

insert image description here

charCodeAt()

  • Get the character encoding (Unicode encoding) of the character at the specified position
var str = "Hello World!!!";
var result = str.charCodeAt(6);
console.log(result);

insert image description here

fromCharCode()

  • You can get characters according to character encoding
var result = String.fromCharCode(18888);
console.log(result);

insert image description here

concat()

  • Can be used to concatenate two or more strings
  • Will not affect the original string
var str = "Hello World!!!";
var result = str.concat("Hello","World");
console.log(result);

insert image description here

indexOf()

  • This method can retrieve whether a string contains the specified content.
  • If the string contains the content, the index of its first occurrence is returned.
  • If the specified content is not found, it returns -1
  • You can specify a second parameter to specify the position to start searching.
var str = "Hello World!!!";
var result0 = str.indexOf('l');
var result1 = str.indexOf('l',3);//Start from the third position var result2 = str.indexOf('l',5);//Start from the fifth position console.log(result0);
console.log(result1);
console.log(result2);

insert image description here

lastIndexOf()

  • The usage of this method is the same as indexof()
  • The difference is that indexOf() searches from the front to the back.
  • And lastIndexOf() searches from the back to the front

slice()

  • You can extract the specified content from the string
  • It will not affect the original string, but will return the intercepted content
  • parameter:
    • First, the index of the starting position (including the starting position)
    • Second, the index of the end position (not including the end position)
    • If the second parameter is omitted, all subsequent
    • You can also pass a negative number as a parameter, and if it is a negative number, it will be calculated from the back
var str = "Hello World!!!";
var result0 = str.slice(0,2);
var result1 = str.slice(1,-4);
console.log(result0);
console.log(result1);

insert image description here

substring()

  • Can be used to intercept a string, similar to slice()
  • It will not affect the original string, but will return the intercepted content
  • parameter:
    • First, the index of the starting position (including the starting position)
    • Second, the index of the end position (not including the end position)
  • Cannot accept negative values ​​as parameters
    • If a negative value is passed, 0 is used by default.
  • Can automatically adjust the parameter position. If the second parameter is smaller than the first one, it will be automatically swapped.
var str = "Hello World!!!";
var result0 = str.substring(0,2);
console.log(result0);

insert image description here

split()

  • You can split a string into an array
  • parameter:
    • It takes a string as a parameter, and the array will be split according to the string.
var str = "He llo Worl d!!!";
var result0 = str.split(' ');
console.log(result0);
console.log(Array.isArray(result0));

insert image description here

toUpperCase()

  • Converts a string to uppercase and returns
var str = "He llo Worl d!!!";
var result0 = str.toUpperCase();
console.log(result0);

insert image description here

toLowerCase()

  • Converts a string to lowercase and returns
var str = "He llo Worl d!!!";
var result0 = str.toLowerCase();
console.log(result0);

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JavaScript built-in object Math case summary analysis
  • Introduction and usage examples of the built-in object Math in javascript
  • Detailed explanation of JS built-in objects and Math objects
  • A brief discussion on the properties and methods of the js built-in object Math (recommended)
  • How to use the built-in object Math of javascript object
  • Detailed explanation of the use of basic methods of Math built-in objects in JavaScript

<<:  CSS box hide/show and then the top layer implementation code

>>:  Hidden overhead of Unix/Linux forks

Recommend

Brief analysis of mysql scheduled backup tasks

Introduction In a production environment, in orde...

mysql 5.7.17 winx64.zip installation and configuration method graphic tutorial

Preface: I reinstalled win10 and organized the fi...

How to connect XShell and network configuration in CentOS7

1. Linux network configuration Before configuring...

Vue.js uses Element-ui to implement the navigation menu

This article shares the specific code for impleme...

Installation and deployment of MySQL Router

Table of contents 01 Introduction to MySQL Router...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

WeChat applet realizes the effect of shaking the sieve

This article shares the specific code of the WeCh...

Vue3 implements CSS infinite seamless scrolling effect

This article example shares the specific code of ...

Detailed explanation of padding and abbreviations within the CSS box model

As shown above, padding values ​​are composite at...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...