javascript various methods
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
- 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!");
- 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");
- 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);
- 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
- 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
- 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"]
- 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"]
- 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
- 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"
- 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
- 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
- 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"]
- 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]"
- 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
- 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)
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

The article discusses defining routes in React Router using the <Route> component, covering props like path, component, render, children, exact, and nested routing.

Vue 2's reactivity system struggles with direct array index setting, length modification, and object property addition/deletion. Developers can use Vue's mutation methods and Vue.set() to ensure reactivity.

Redux reducers are pure functions that update the application's state based on actions, ensuring predictability and immutability.

The article discusses Redux actions, their structure, and dispatching methods, including asynchronous actions using Redux Thunk. It emphasizes best practices for managing action types to maintain scalable and maintainable applications.

TypeScript enhances React development by providing type safety, improving code quality, and offering better IDE support, thus reducing errors and improving maintainability.

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.
