An array of
An Array is a collection of elements (usually of the same type) arranged in a certain order, which is called an Array. The element type of the array is unlimited, and the length can be adjusted dynamically.
// Create an empty array
var arr = [];
// Create an array containing multiple data types. The data type is unlimited
var arr2 = [1.true.false.null.undefined."haha"[7.8].9.10];
// Get arR2 entries with subscripts 0 and 1
console.log(arr2[0]);
console.log(arr2[1]);
// Change the value of an item in the array
arr2[4] = 5;
// Get the length of the array
console.log(arr2.length);
// Get the value of the last item in the array
console.log(arr2[arr2.length - 1]);
// Assign a value to the length property to force the array length to change
// Greater than the subscript, the extra term is empty. Less than the subscript, the following data items will be directly deleted, and irreversible
arr2.length = 12;Arr2 [10] and arR2 [11] are null
console.log(arr2);
arr2.length = 10;Arr2 [10], ARR2 [11] data are deleted
console.log(arr2);
// Add an item greater than the maximum subscript to force an increase in the array length
arr2[14] = 13;
Copy the code
Array traversal
var arr = [45.56.76.88.89.90.100.34.56];
// Add 5 to each item in the array
for (var i = 0; i < arr.length ; i++) {
arr[i] += 5;
}
console.log(arr);
Copy the code
function
Function, also known as function or method, encapsulates a piece of code together. The encapsulated function has a special function. The function encapsulates a piece of code that can be reused in the future.
// Functions must be defined before they can be used
// Function names can be named using letters, digits, underscores (_), and $($). Digits cannot start with digits, and are case-sensitive. Keywords cannot be used, the same as variable names
// Function declaration
function fun() {
console.log(1);
console.log(2);
console.log(3);
}
// Function call
fun();
Copy the code
Parameters of a function
The function reserves an interface to allow the user to customize the content so that the function can be executed differently.
Interface: is the parameter of the function, the essence of the function parameter is a variable, can receive any type of data, resulting in the function execution results according to different parameters, the result is also different.
Arguments and parameters are explained as follows:
// Define a summation function, passing in two data
// Parameters: pass two parameters, the data type is number
// Get the sum of two numbers
function sum(a,b) {
console.log(a + b);
}
// Call the function
sum(3.4);
Copy the code
The return value of the function
// Use the return value to create the function after the end of the run
function sum(a,b) {
return a + b;
}
// Assign the return value to the variable
var num = sum(3.4);
console.log(num);
// Assign the return value to the function argument
console.log(sum(2,sum(3.4)));
Copy the code
Functional expression
Delta is another way of defining a function. Definition method: is the function definition, anonymous function assigned to a variable. Assigning a function definition to a variable reduces the function as a whole to an expression. Anonymous functions: Functions have no function name. Call a function expression by adding () to a variable name, not by using the function name.
// Define a function expression
var foo = function fun() {
console.log(1);
};
var foo2 = function () {
console.log(2);
};
// The function can only be called by the variable name. The function name cannot be called successfully
foo();
foo2();
fun(); // The call was unsuccessful
Copy the code
The data type of the function
A Function is a separate data type Function. Because a function is a data type, it can participate in other programs. For example, a function can be called from another function as an argument to another function. Alternatively, a function can be returned from within the function as a return value.
// A function is a data type that can be used as an argument to other functions
setInterval(function(){
console.log(1);
},1000);
// treat a function as the return value of another function
function fn(b) {
var a = 10;
return function () {
alert(a + b);
};
}
Copy the code
The arguments object for the function
In JavaScript, the Arguments object is a special object that is actually a built-in property of the current function. That is, all functions come with a built-in arguments object that stores all arguments passed. Arguments is a pseudo-array, so arguments can be iterated over. The number of arguments to a function can be different from the number of parameters. All arguments are stored in an array object of the arguments class inside the function.
// Define a function
function sum(a,b) {
return a + b;
}
// When calling a function, the number of arguments can be different from the number of parameters
console.log(sum(1.2));
console.log(sum(1));
console.log(sum(1.2.3.4));
// There is a arguments object inside the function that takes all arguments
function fun() {
console.log(arguments);
console.log(arguments.length);
// Use array traversal to get each argument
for (var i = 0 ; i <= arguments.length - 1 ; i++) {
console.log(arguments[i]); }}// Call the function
fun(1.2.3.4.5.6.7);
Copy the code
Function recursive
Function recursion is the way in which a function itself can be called from within the function name. Too many recursions are prone to errors: more than the computer can calculate. More often, recursion is used to solve mathematical phenomena. For example, you can print the value of a term in the Fibonacci sequence.
// Fibonacci sequence
// Parameter: positive integer
// Return value: the Fibonacci sequence value of the corresponding integer position
function fibo(a) {
if (a === 1 || a === 2) {
return 1;
} else {
return fibo(a - 1) + fibo(a - 2); }}// Call the function
console.log(fibo(1));
console.log(fibo(2));
console.log(fibo(3));
console.log(fibo(4));
console.log(fibo(5));
console.log(fibo(6));
Copy the code
scope
Scope: The scope within which a variable can act. If a variable is defined inside a function, it can only be accessed inside the function, and cannot be used outside the function. The function is the scope of the variable definition. Any structure in curly braces {} belongs to a block, and all variables defined within it are invisible outside the code block, called block-level scope. Prior to ES5, there was no concept of block-level scope, only function scope. At this stage, JavaScript can be considered to have no block-level scope.
Global and local variables
Local variable: a variable defined inside a function that can only be accessed inside the function scope. Global variable: in a broad sense, is also a local variable, defined in the global variable, scope scope is global, in the whole JS program any position can be accessed. Variables are destroyed when they are out of scope. Global variables are not destroyed until the web page or browser is closed.
Scope of arguments and functions
The parameter of a function is essentially a variable and has its own scope. The parameter of a function is also a local variable within the function itself, which can only be used inside the function and is not defined outside the function.
// Function parameters are also local variables
function fun(a) {
a = 2;
console.log(a);// The argument a can be called inside the function
}
// Call the function
fun(1);
//console.log(a); // The argument a cannot be called outside the function, an error is reported
Copy the code
Functions also have their own scope.
// Functions also have their own scope
function outer() {
var a = 1;
function inner() {
console.log(2);
}
// Call the subfunction inner() to succeed
inner();
}
// Call the function
outer();
//inner(); Inner () cannot be called outside the function
Copy the code
Scope chains and shadowing effects
- The scope chain
Only functions can make scoped structures, so any code must have at least one scope, the global scope. Whenever code has a function, that function constitutes another scope. If there are functions within a function, then another scope can be created within that scope. By listing all such scopes, you can have a structure: a chain structure that points from inside the function to outside the function. This is called the scope chain.
- Shading effect
Procedure in case of a variable, use scope when find order, different levels of function are likely to define variables of the same name, a variable when use, will from his first layer to find the variable scope, if the current layer is not variable definitions according to the order from this layer to find, in turn until you find the first variable definitions. In the whole process, the effect of the inner variables overshadowing the outer variables will occur, which is called “overshadowing effect”.
// Global scope
var a = 1;
// Create a function
function outer() {
var a = 2;
// Inner function
function inner() {
var a = 3;
console.log(a);A =2; a=1; a=2; a=1;
}
inner();
console.log(a);// a=2; // A =1; // A =2; // A =2
}
/ / call
outer();
Copy the code
Effects of not writing the keyword var in functions
If you want to define a new variable inside a function, if you do not add the keyword var, it is equivalent to defining a global variable. If the global identifier is the same, it will be affected by variables inside the function, and local variables will pollute the global variable. Note: the var keyword must be written every time a variable is defined, otherwise it will be defined globally and may pollute it.
var a = 1;
function fun() {
a = 2;// There is no keyword var, which is a global variable
console.log(a);// The output is 2
}
fun();
console.log(a);// The output is 2 instead of 1 because it is polluted by fun()
Copy the code
Pre-parsing and declaration enhancement
The execution of JavaScript code is performed by the JavaScript parser in the browser. When a JavaScript parser executes JavaScript code, there are two processes: the pre-parsing process and the code execution process. Pre-resolution process: 1, the variable declaration to the current scope of the first, only improve the declaration, not improve the assignment. 2. Elevating the function declaration to the front of the current scope only elevates the declaration, not the call. 3. Improve var first, then function.
Variable declaration promotion
JavaScript execution process: after pre-parsing, the root of the new code order, from top to bottom in accordance with the established rules of js code execution. During pre-parsing, all defined variables are promoted to the top of their scope. In future code execution, the promoted variable procedure is executed first. In the promotion process, only the promotion declaration process, does not promote the variable assignment, equivalent to the variable definition is not assigned, the variable store undefined value. As a result, there is a phenomenon in JS that variables defined after previous calls will not report errors, only undefined value will be used.
var a = 1;
function fun() {
console.log(a);//a is declared in the scope chain at this level (the declaration will be first in the scope whenever it is preprocessed at this level), so a is used at this level, but before assignment, so undefined is printed
var a = 0;
}
fun();
Copy the code
Function declaration promotion
The functions defined will be declared at the top of the scope, and the promoted function declaration process will be executed first in order of future code execution. During the pre-parsed code execution, the function definition process is already executed at the very beginning, and once the function definition is successful, the function can be called directly afterwards. Therefore, there will be a phenomenon in JS, the function defined after the previous call, will not report an error, and can normally execute the code inside the function.
// Call the function first
fun();
// Define the function
function fun() {
console.log(2);
}
Copy the code
Ascending order
In the pre-parsing process, the var variable declaration is promoted first, and then the function function declaration is promoted. If the variable name is the same as the function name, then the promoted function name identifier will override the promoted variable name, so that when the call identifier appears in the subsequent code, it is the function definition process, not undefined. If the process of calling an identifier after the function and variable definition in source code overwrites the variable name once, then the new value overwrites the value of the function when the variable is assigned, then the identifier is called again with the new value of the variable. Suggestion: Do not write the same identifier for variable or function names to avoid overwriting.
var fun = "haha";
// Define the function
function fun() {
console.log(2);
}
fun();// Error type error, fun is not a function
Copy the code
Enhancement of function expressions
Function expressions are promoted by variable declaration rather than function declaration during pre-parsing. After the promotion, an undefinedo is stored inside the variable to call the function method in front, and the data type will prompt an error. Suggestion: When defining functions, use the function keyword definition so that the function declaration enhancement takes effect forever.
console.log(foo);/ / output is undefined
foo();Foo is not a function
// Function expressions perform variable declaration promotion
var foo = function () {
console.log(3);
};
Copy the code
Application of function declaration enhancement
Function declaration promotion can be used to reorder code so that large chunks of definition are placed at the end of the code without affecting code execution.
IIFE calls the function itself
IIFE: immediately – invokedfunctionexpression, called the function called instant expression, also called the calling function, said function in the call as soon as defined. How a function is called: The function name or the variable name of a function expression is followed by the () operator. The function name cannot be used to implement immediate self-call. The function expression can be used to implement immediate execution. The reason is that during the function expression definition, a function is shortened to an expression followed by the () operator. Inspiration: If you want to implement IIFE, you can find a way to dwarf functions into expressions.
Method of reducing a function to an expression
To reduce a function to an expression, you can have the function participate in some operations, that is, add some operators in front of the function. Mathematical operator: +-() Logical operator:! A non-operational IIFE structure can close the scope of a function and cannot call a function outside the structure. The most common use of IIFE is the () operator, and functions can be written without function names, using anonymous functions.
// Common IIFE structure
(function (a) {
console.log(a); }) (4);
// The keyword is defined in a way that cannot be executed immediately
//function fun() {
// console.log(1);
/ /} ();
// Function expressions can be executed immediately at definition time
var foo = function fun() {
console.log(2); } ();// We can reduce a function to an expression by adding an operator in front of it
+ function fun() {
console.log(1); } ();// -, (),! So can operators
Copy the code
object
Objects in JavaScript:
An object in JavaScript is an abstraction of an object in your life.
JavaScript objects are unordered collections of properties that can contain base values, objects, or functions. An object is an unordered set of values. We can think of objects in JavaScript as key-value pairs, where values can be data and functions.
Behavior and characteristics of objects:
Features – represented by attributes in objects;
Behavior – represented by methods in objects.
Object literals
The easiest way to create an object is to use object literals to assign values to variables. It’s like an array. Object literal syntax: {} can store multiple pieces of data inside, separated by commas, do not put a comma after the last one. Each piece of data consists of an attribute name and value. The key-value pair is written as k:v. K: the attribute name and value can be any type of data, such as simple type data, functions, and objects. Distinguish between properties and methods:
- Attribute: A descriptive characteristic of an object, usually a noun, that corresponds to a variable defined inside the object.
- Methods: The behavior and function of an object, usually a verb, defined as a function within the object.
// Create an object with an object literal
var person1 = {
name : "zs".age : 18.sex : "male".sayHi : function () {
console.log(this.name + "Say hello to you.");// The internal call is called with the this. attribute name}};Copy the code
Object data calls and changes
// Call the properties and methods of the object
console.log(person1.name);
console.log(person1.age);
console.log(person1.sex);
person1.sayHi();
// The parentheses call the method
console.log(person1["name"]);
person1["sayHi"] (); person1.sayHi();// Change the data
person1.age = 19;
// Add new data
person1.weight = 140;
// Delete attributes
delete person1.sex;
console.log(person1);
Copy the code
Other ways to create objects
The new Object() constructor creates the Object
The Object() constructor is a special kind of function. It is used primarily to initialize an object when it is created, that is, to assign initial values to its member variables. It is always used with the new operator in the statement that creates the object.
- Constructors are used to create a class of objects that begin with a capital letter.
- Constructors have to be used with new to make sense. Without new, constructors are just like normal functions and meaningless.
When executed, new does four things:
- New creates a new empty object in memory
- New will make this point to this object
- Constructor purpose: To add attributes and methods to the new object
- New will return this new object
// Create the new Object() method
var person1 = new Object(a);// create a new empty object
// Add attributes and methods
person1.name = "zs";
person1.age = 18;
person1.sex = true;
person1.sayHi = function () {
console.log("Hello");
};
Copy the code
The factory function method creates the object
If you want to create more than one similar object, you can wrap the newObject() procedure in a function that can be called later to create an object, equivalent to a function factory that produces objects, to simplify the code.
// The factory method is a wrapper around the new Object() method
function createPerson(name,age,sex) {
// Create an empty object
var person = new Object(a);// Add attributes and methods that can accept parameter values
person.name = name;
person.age = age;
person.sex = sex;
person.sayHi = function () {
console.log("hello");
};
// Use the object as the return value of the function
return person;
}
// To create an object, call the factory function
var p1 = createPerson("zs".18.true);
var p2 = createPerson("ls".19.false);
/ / output
console.log(p1);
console.log(p2);
Copy the code
Custom constructor methods create objects
Much simpler than the factory approach. A custom constructor to create a specific object, the function does not need to new a constructor process, directly use this instead of object properties and methods to write, also do not need to return a return value. When used, call a custom constructor using the new keyword. Note: Constructor function names must be capitalized to distinguish them from other normal function names.
// Define your own constructor
function Person(name,age,sex) {
// There is no need to use new for a new object
// Replace new objects created in the future with this
this.name = name;
this.age = age;
this.sex = sex;
this.sayHi = function () {
console.log("hello");
};
// No need to add return
}
// Call the constructor with the new keyword
var p1 = new Person("zs".18.true);
Copy the code
Object traversal method
// Create an object with an object literal
var person1 = {
name : "zs".age : 18.sex : "male".sayHi : function () {
console.log(this.name + "Say hello to you."); }};// Iterate over the object
for (var k in person1) {
// k stores the attribute name or method name of each piece of data
console.log(k + "The attribute value of the attribute is" + person1[k]);
}
Copy the code
Simple data types and complex data types
The difference between simple data types and complex data types: Simple data types are also called value types, and complex data types are also called reference types. Value types: Simple data types, basic data types, when stored, the variable stores the value itself, so they are called value types. Reference types: Complex data types. When stored, only the address (reference) is stored in the variable, so they are called reference data types.
// Base data type
var a = 5;
var b = a; // A copies data 5 stored in A to B
a = 10;
console.log(a);
console.log(b);
// Complex data types
var p1 = {
name : "zs".age : 18.sex : "male"
};
var p = p1; //p1 copies the internal stored address pointing to the object prototype to P
// There is a linkage relationship between two variables. One change will cause another change
p.name = "ls";
console.log(p);
console.log(p1);
// Arrays and functions are also stored addresses when stored in variables
var arr = [1.2.3.4];
var arr2 = arr;
arr[4] = 5;
console.log(arr);
console.log(arr2);
Copy the code
Built-in objects
There are three types of JavaScript objects: custom objects, built-in objects, browser objects (BOM) ECMAscript objects: custom objects, built-in objects Use a built-in object, just need to know what members of the object, what functions, directly use. Refer to the manual W3C/MDN. MDN is more detailed.
console.log(Math);
Copy the code
The Math object
The Math object has properties and methods of mathematical constants and functions that we can use directly. Find members of Math based on mathematically related operations (take absolute value, take round).
Two ways to create an array object
Arrays are also objects. There are two ways to create an array object:
- Literal way []
arr = ['12'.'Jack'.'Rose'];
Copy the code
- New Array constructor method.
// Arrays are also objects that can be generated by constructors
/ / an empty array
var arr1 = new Array(a);// Add data and pass parameters directly
var arr2 = new Array(1.2.3);
var arr3 = new Array("zs"."ls"."ww");
// Check the data type of array
console.log(typeof(arr));/ / output Object
console.log(typeof(arr3));/ / output Object
var a = {};
// Check whether an instance object belongs to an object type
console.log(arr1 instanceof Array);/ / output true
console.log(arr2 instanceof Array);/ / output true
console.log(a instanceof Array);/ / output is false
console.log(fun instanceof Function);/ / output true
function fun () {
console.log(1);
}
Copy the code
Common methods for array objects
ToString () converts an array to a string, concatenated with English commas
arr = ['1'.'2'.'3'];
console.log(arr.toString());
Copy the code
Join () hyphenates each item in the array to a complete string using the parameter as a hyphen
Var arr = [1,2,3,4,5,6,7,8,9,10]; Var STR = arr.join(); Console. log(STR) is connected with a comma by default. str = arr.join(""); // Pass an empty string so that the contents of the array are concatenated into the string console.log(STR) without spacing;Copy the code
Push () adds one or more elements to the end of the array, and returns the length of the array. Pop () deletes the last item, shift() deletes the first item, and unshift() adds one or more elements to the beginning of the array, and returns the new length of the array
// the literal method
var arr = [1.2.3.4];
// Move the first item of the array to the last
// Delete the first item
// Add the deleted item to the last item
arr.push(arr.shift());
console.log(arr);
arr.push(arr.shift());
console.log(arr);
arr.push(arr.shift());
console.log(arr);
arr.push(arr.shift());
console.log(arr);
Copy the code
The concat() method merges the two arrays into a new one, leaving the original array untouched. The argument position can be an array literal, an array variable, or a stray value.
The slice(start,end) method extracts a new array from the current array, leaving the original array intact and returning a new array containing elements from start to end (excluding the element). A positive value indicates the subscript position, a negative value indicates the number of positions from the back to the front, and only one parameter can be passed, indicating that the parameter is cut from the beginning position to the end of the string.
// the literal method
var arr = [1.2.3.4.5.6.7.8.9.10];
// Merge methods
// Parameters: array, array variables, scattered
// Return value: a new concatenated array
var arr1 = arr.concat([5.6.7]);
var ar = [8.9.10]
arr1 = arr.concat(ar);
arr1 = arr.concat(11.12.13);
console.log(arr);
console.log(arr1);
// Split method
// The argument is positive
arr1 = arr.slice(3.7);
// The argument is negative
arr1 = arr.slice(-7, -1);
// Write only one argument
arr1 = arr.slice(7);
console.log(arr);
console.log(arr1);
Copy the code
Splice and location method to delete, insert, replace, splice (index, howmany elementl, element2,…). Howmany: The number of elements to be removed, which can be 0 element1, element2… : The new data to replace. IndexOf () looks for the first indexOf the data in the array. LastlndexOf () looks for the last indexOf the data in the array. Note: returns -1 if not found
// the literal method
var arr = [1.2.3.4.5.6.7.8.9.10];
// Delete function, pass the first two parameters
console.log(arr.splice(2.5));
console.log(arr);
// Replace function, pass 3 or more parameters
arr.splice(2.5."haha"."hello");
console.log(arr);
// Insert function, pass 3 or more parameters, but the second parameter must be 0
arr.splice(2.0."hello");
console.log(arr);
// Find the index of the first occurrence of an element in the array from front to back
console.log(arr.indexOf(4));
// Find the index of the last occurrence of an element in the array from front to back
console.log(arr.lastIndexOf(4));
console.log(arr.lastIndexOf(11));
Copy the code
Reverse order: Reverse () completely reverses the array so that the first item becomes the last and the last item becomes the first.
var arr = [1.2.3.4.5.6.7.8.9.10];
console.log(arr.reverse())
Copy the code
Sorting: sort (); If you want to sort by numeric size, you must add the comparison function argument so. This function compares two values and returns a number that indicates the relative order of the two values. The comparison function should have two parameters a and b. According to the relationship between A and B as the judgment condition, the return value can be divided into three branches according to the condition, positive number, negative number and 0:
- The return value is negative -1: A comes before B.
- The return value is the integer 1: A comes after B.
- The return value is 0: the order of a and B remains the same.
What man can control is the judgment condition.
// Sort, by default, from smallest to largest in character encoding order
arr.sort();
console.log(arr);
// Add an argument to the comparison function
arr.sort(function(a,b){
if (a < b) {
return -1; // indicates that a comes before B
} else if (a > b) {
return 1; // a comes after B
} else {
return 0; // indicates that a and B remain unchanged}});console.log(arr);
Copy the code
Several ways to empty an array
arr = []; Arr. Length = 0; Arr. Splice (0, arr. LengthCopy the code
Basic packing type
To make it easier to manipulate simple data types, JavaScript also provides a special simple type object: the String primitive type has no methods. When methods such as str.substring() are called, STR is wrapped as a temporary object of type String, substring is called, and the temporary object is destroyed.
// Object data types: have properties and methods // But strings are allowed to call some properties and methods. Var str2 = STR. Slice (3, 5); console.log(str2); Var str3 = new String("abcdef"); var str3 = new String("abcdef"); console.log(str); console.log(str3); Var STR = "This is a string "; Var str4 = new String(STR); Var str2 = str4. Slice (3, 5); str4 = null;Copy the code
string
Strings are immutable. All of the string methods do not modify the string itself (the string is immutable) and return a new string upon completion. Because strings are immutable, they take up extra memory and are inefficient, causing problems when concatenating large numbers of strings.
String properties
Length attribute: str.length String length refers to the total number of characters in a string.
The charAt() method returns a character at a specified position. Char :charator at: where the argument is the index of the string. It starts at 0. A character that returns the specified subscript position.
The indexOf() method returns the first occurrence of a specified string value in a string. Finds the index of the first occurrence of the specified substring in the original string. If the substring is not present in the original string, the return value is -1
The concat() method is used to join two or more strings. Arguments are flexible and can be strings, or string variables, or multiple strings. A new string is generated; the original string does not change.
The split() method is used to split different strings into an array of strings. The parameter part is the separator, which is used to split the string into parts that make up the array as each item of the array. If the separator is an empty string, it splits each character into each item in the array.
ToLowerCase () converts strings toLowerCase toUpperCase() converts strings toUpperCase converts all English characters toUpperCase or lowercase. A new string is generated, the original string does not change.
The slice() method extracts a portion of a string and returns the extracted portion in a new string. Syntax: Slice (start,end) a string from the start position to the end position < excluding the end position). A positive value indicates the subscript position, a negative value indicates the number of positions from the back to the front, and only one parameter can be passed, indicating that the parameter is cut from the beginning position to the end of the string.
The substr() method extracts a specified number of characters from the start subscript in a string: substr(start, howmany) extracts a specified length of string from the start position. The start parameter distinguishes between positive and negative values. A positive value indicates the subscript position, and a negative value indicates the number of positions from back to front. The howmany parameter must be a positive number and may not be written, which means that the value is intercepted from start to the end.
The substring() method is used to extract the character of a string intermediate between two specified subscripts. Syntax: The substring(start,end) parameter can only be a positive number. The size of the two parameters is not limited. Before executing the method, the size of the two parameters will be compared. The smaller value will be used as the start position, and the larger value will be used as the end position. If no second argument is written, it is truncated from the beginning to the end of the string.
Var STR = "this is a normal string, ABC, ¥%#"; // The length attribute console.log(str.length); //charAt() returns the character console.log(str.charat (6)) at the specified subscript position; //indexOf() returns the subscript console.log(str.indexof (" string ")) where the substring first appears in the original string; Var str2 = str.concat(" ha ha ha "," normal "); console.log(str); console.log(str2); //split() split string into an array var arr = str.split(", "); console.log(arr); Var arr = str.split(""); arr.reverse(); str = arr.join(""); str = str.split("").reverse().join(""); console.log(str); Var str1 = str.toupperCase (); var str2 = str1.toLowerCase(); console.log(str); console.log(str1); console.log(str2); Var str1 = str.slice(3,7); var str1 = str.slice(3,7); var str1 = str.slice(-7,-3); var str1 = str.slice(-7); console.log(str1); Var str2 = str.substr(3,7); Var str2 = STR. Substr (9, 7); var str2 = str.substr(-9); console.log(str2); Var str3 = str.substring(3,7); //substring(start,end) is an integer. Var str3 = STR. The substring (7, 3); var str3 = str.substring(7); console.log(str3);Copy the code