javascript various methods

王林
Release: 2023-05-09 13:46:38
Original
581 people have browsed it

JavaScript is a widely used programming language that can be used in many fields such as web development, game development, and mobile application development. Methods are a very important part of JavaScript development. This article will introduce various methods in JavaScript and demonstrate the application of these methods through examples.

Part 1: Basic method

  1. alert() method

alert() method is used to pop up a dialog box in the browser page, usually Used to display information or warnings to users.

Syntax:

alert("message");

Example:

alert("Welcome to my website!");

  1. prompt() method

prompt() method is used to pop up a dialog box to prompt the user to enter information. It returns the value entered by the user (of type string).

Syntax:

prompt("message","default value");

Example:

let name = prompt("Please enter your Name: ","Zhang San");

  1. console.log() method

console.log() method is the most commonly used method when debugging code. Use to output information on the console. It can output data of types such as strings, variables, objects and arrays.

Syntax:

console.log("message");

Example:

let num = 10;
console.log(" The number is " num);

  1. parseInt() method

parseInt() method is used to convert a string to an integer type. If conversion is not possible, NaN is returned.

Syntax:

parseInt(string, radix);

string: String to be converted to an integer.

radix: base conversion, optional. Defaults to 10 if not set.

Example:

let str = "123";
let num = parseInt(str);
console.log(num); //123

  1. parseFloat() method

The parseFloat() method is used to convert a string to a floating point number type. If conversion is not possible, NaN is returned.

Syntax:

parseFloat(string);

string: The string to be converted to a floating point number.

Example:

let str = "3.14";
let num = parseFloat(str);
console.log(num); //3.14

Part 2: Array methods

  1. push() and pop() methods

push() method is used to add an element at the end of the array, while pop() method Used to delete elements at the end of an array.

Syntax:

push(newelement);

pop();

Example:

let fruits = ["Apple" ,"Banana","Orange"];
fruits.push("Grape");
console.log(fruits); //["Apple","Banana","Orange","Grape" ]
fruits.pop();
console.log(fruits); //["Apple","Banana","Orange"]

  1. shift() and unshift( )Method

The shift() method is used to delete an element at the beginning of the array, while the unshift() method is used to add an element to the beginning of the array.

Syntax:

shift();

unshift(newelement);

Example:

let fruits = ["Apple" ,"Banana","Orange"];
fruits.unshift("Grape");
console.log(fruits); //["Grape","Apple","Banana","Orange" ]
fruits.shift();
console.log(fruits); //["Apple","Banana","Orange"]

  1. slice() method

The slice() method is used to intercept a segment of elements from an array. Note that it does not modify the original array, but returns a new array.

Syntax:

slice(start, end);

start: starting position, including this position.

end: End position, excluding this position.

If end is omitted, it will be intercepted from the starting position to the end of the array.

Example:

let fruits = ["Apple","Banana","Orange","Grape"];
let newfruits = fruits.slice(1,3);
console.log(newfruits); //["Banana","Orange"]

Part 3: String method

  1. toUpperCase() and toLowerCase() methods

The toUpperCase() method is used to convert a string to uppercase format, while the toLowerCase() method is used to convert a string to lowercase format.

Syntax:

toUpperCase();

toLowerCase();

Example:

let str = "Hello World";
let newstr1 = str.toUpperCase();
let newstr2 = str.toLowerCase();
console.log(newstr1); //"HELLO WORLD"
console.log(newstr2); //"hello world"

  1. indexOf() and lastIndexOf() methods

indexOf() method is used to get the position of a specified character or string in a string. If not found, returns -1. The lastIndexOf() method is similar to the indexOf() method, but starts searching from the end of the string.

Syntax:

indexOf(searchvalue, start);

searchvalue: The value to be found, which can be a character or a string.

start: optional parameter. Which index to start searching from.

lastIndexOf(searchvalue, start);

Example:

let str = "Hello World";
let pos1 = str.indexOf("l");
let pos2 = str.lastIndexOf("l");
console.log(pos1); //2
console.log(pos2); //9

  1. concat() method

The concat() method is used to concatenate multiple strings to generate a new string.

Syntax:

concat(string1, string2, ..., stringn);

Example:

let str1 = "Hello";
let str2 = "World";
let str3 = str1.concat(" ", str2);
console.log(str3); //"Hello World"

Part 4: Object method

  1. keys() and values() methods

The keys() method is used to get all the keys in the object, and the values() method is used to get all the values in the object. They both return an array.

Syntax:

Object.keys(object);

Object.values(object);

Example:

let obj = {name:"Zhang San",age:18,city:"Beijing"};
let keys = Object.keys(obj);
let values = Object.values(obj);
console .log(keys); //["name","age","city"]
console.log(values); //["Zhang San",18,"Beijing"]

  1. toString() method

The toString() method is used to convert objects into strings and is often used for debugging and logging.

Syntax:

object.toString();

Example:

let obj = {name:"张三",age:18,city :"Beijing"};
console.log(obj.toString()); //"[object Object]"

  1. hasOwnProperty() method

The hasOwnProperty() method is used to check whether a certain property exists in the object. Returns true if present, false otherwise.

Syntax:

object.hasOwnProperty(property);

property: The name of the property to be checked.

Example:

let obj = {name:"Zhang San",age:18,city:"Beijing"};
console.log(obj.hasOwnProperty("name" )); //true
console.log(obj.hasOwnProperty("gender")); //false

Part 5: Date method

  1. Date() method

Date() method is used to get or set the date and time. If no parameters are passed, the current date and time is returned.

Syntax:

new Date();

Example:

let date = new Date();
console.log(date) ; //Wed Aug 04 2021 15:41:10 GMT 0800 (China Standard Time)

  1. getDate(), getMonth() and getFullYear() methods

getDate () method is used to get the current date (the day of each month), while the getMonth() method is used to get the current month (0 means January, 11 means December), and the getFullYear() method is used to get the current month. years.

Syntax:

getDate();

getMonth();

getFullYear();

Example:

let date = new Date();
let day = date.getDate();
let month = date.getMonth() 1;
let year = date.getFullYear();
console.log(year "-" month "-" day); //"2021-8-4"

Summary:

This article introduces the basic methods and array methods commonly used in JavaScript , string methods, object methods and date methods, and corresponding examples are given, hoping to be helpful to readers. Of course, there are far more methods in JavaScript than these. If you want to learn more, it is recommended to consult more documents or reference books and practice them.

The above is the detailed content of javascript various methods. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!