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

Sample code for implementing 3D rotation effect using pure CSS

Mainly use the preserve-3d and perspective proper...

Solve the problem of using swiper plug-in in vue

Since I used this plugin when writing a demo and ...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

jQuery canvas draws picture verification code example

This article example shares the specific code of ...

How to insert weather forecast into your website

We hope to insert the weather forecast into the w...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...

Pure CSS to achieve the list pull-down effect in the page

You may often see the following effect: That’s ri...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

MySQL database operations (create, select, delete)

MySQL Create Database After logging into the MySQ...

Differences between MySQL CHAR and VARCHAR when storing and reading

Introduction Do you really know the difference be...

Detailed explanation of several examples of insert and batch statements in MySQL

Table of contents Preface 1.insert ignore into 2....

Implementation of select multiple data loading optimization in Element

Table of contents Scenario Code Implementation Su...

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...