Type String

A string is a variable that stores characters such as “Keafmd”.

The string can be any text in quotes. You can use single or double quotation marks.

<script type="text/javascript">
    var name = '牛哄哄的柯南';
</script>

Copy the code

String properties and methods

Raw value strings, such as “John”, have no attributes and methods (because they are not objects).

Raw values can use JavaScript properties and methods because JavaScript can treat raw values as objects when executing methods and properties.

String properties

attribute instructions
constructor Returns a function that creates a string attribute
length Returns the length of the string
prototype Allows you to add properties and methods to objects

String method

methods describe
charAt() Returns a character at the specified index position
charCodeAt() Returns the Unicode value of the character at the specified index position
concat() Concatenate two or more strings, return the concatenated string, producing a new string
fromCharCode() Converts Unicode to a string
indexOf() Returns the position in a string to retrieve the first occurrence of the specified character
lastIndexOf() Returns the position in a string that retrieves the last occurrence of the specified character
localeCompare() Compare two strings in a locally specific order
match() Finds a match for one or more regular expressions
replace() Replaces the substring that matches the regular expression
search() Retrieves the value that matches the regular expression
slice() Extracts a fragment of a string and returns the extracted portion in a new string
split() Splits a string into substring arrays
substr() Extracts a specified number of characters from the initial index number in the string
substring() Extracts the character between two specified index numbers in a string
toLocaleLowerCase() Converts strings to lowercase based on the host’s locale
toLocaleUpperCase() Converts the string to uppercase depending on the host’s locale
toLowerCase() Converts a string to lowercase
toString() Returns a string object value
toUpperCase() Converts the string to uppercase
trim() Removes whitespace at the beginning and end of a string
valueOf() Returns the original value of a string object

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var msg = 'hello javascript'; Console. log(' last character of string: '+ msg.charat (msg.length-1)) var str1 = "hello "; var str2 = "world!" console.log("str1.concat(str2):"+str1.concat(str2)) console.log("str1: "+str1); console.log("str2: "+str2); var str3 = "hello world!" console.log("str3.indexOf('l'): "+str3.indexOf('l')) console.log("str3.lastIndexOf('l'): "+str3.lastIndexOf('l')) var str4 ="Hello My Name is Keafmd,Age is 18" console.log('str4.slice(6): '+ str4. Slice (6)). The console log (' str4. Slice (17) : "+ str4. Slice (17, 23) the console. The log (' str4. Slice (- 2, str4. Length) : '+ str4. Slice (- 2, str4. Length)). The console log (" str4. Substr (17, 6) : "+ str4. Substr (17, 6)) console. The log (" str4. Substring (17, 6) : "+ str4. Substring (17, 23)) to the console. The log (" str4. ToUpperCase () :" + (str4. The toUpperCase ())) the console. The log (" str4. ToLowerCase () : "+(str4.toLowerCase())) console.log("str4: "+str4); var str5 =" Hello " console.log("str5.length: "+str5.length); console.log("str5.trim().length: "+str5.trim().length); </script> </head> <body> </body> </html>Copy the code

Effect screenshot:

JavaScript has only one numeric type. Numbers can be numbered with or without a decimal point.

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript"> var age = 18; Var number = 34.7 console.log('age:'+age) console.log('number:'+number) </script> <title></title> </head> <body> </body>  </html>Copy the code

Effect screenshot:

Arithmetic operator

The operator describe example X result Y result
+ add x=y+2 7 5
subtraction x=y-2 3 5
* The multiplication x=y*2 10 5
/ division x=y/2 2.5 5
% Modulo (remainder) x=y%2 1 5
++ Since the increase x=++y 6 6
++ Since the increase x=y++ 5 6
Since the reduction of x=- -y 4 4
Since the reduction of x=y- – 5 4

1. Increment (++) and decrement (- -) : put the variable in front of the limited participation in the operation, and then with the expression calculation.

2. If + participates in a string operation, the result is returned as a string.

3. When numeric and bool operations are performed, bool is converted to a number, true: 1 false: 0.

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var numa = 5; var numb = 6; var numc = 10; console.log("numa+numb: "+(numa+numb)); console.log("numa-numb: "+(numa-numb)); console.log("numa*numb: "+(numa*numb)); console.log("numa/numb: "+(numa/numb)); console.log("numc%numb: "+(numc%numb)); var numd = 2; console.log(++numd); console.log(numd); console.log('1'+'1') console.log("true+1 : "+(true+1)); console.log("false+1 : "+(false+1)); </script> </head> <body> </body> </html>Copy the code

Effect screenshot:

The assignment operator

The assignment operator is used to assign values to JavaScript variables.

Given x=10 and y=5, the following table explains the assignment operator: ↓

The operator example Is equivalent to Operation results
= x=y x=5
+ = x+=y x=x+y x=15
– = x-=y x=x-y x=5
* = x*=y x=x*y x=50
/ = x/=y x=x/y x=2
% = x%=y x=x%y x=0

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var numa = 100; var numb = numa; console.log("numa: "+numa); console.log("numb: "+numb); numa += 10; console.log("numa: "+numa); numa -= 10; console.log("numa: "+numa); numa *= 2; console.log("numa: "+numa); numa /= 5; console.log("numa: "+numa); numa %= 38; console.log("numa: "+numa); </script> </head> <body> </body> </html>Copy the code

Effect screenshot:

Comparison operator

Comparison operators are used in logical statements to determine whether variables or values are equal.

Given x=5, the following table explains the comparison operator: ↓

The operator describe To compare The return value
= = Is equal to the x==8 false
= = Is equal to the x==5 true
= = = Absolutely equal (value and type equal) X = = = “5” false
= = = Absolutely equal (value and type equal) x===5 true
! = Is not equal to x! = 8 true
! = = Not absolutely equal (value and type are not equal, or both are not equal) x! = = “5” true
! = = Not absolutely equal (value and type are not equal, or both are not equal) x! = = 5 false
> Is greater than x>8 false
< Less than x<8 true
> = Greater than or equal to x>=8 false
< = Less than or equal to x<=8 true

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var numa = 100; var numb = 150; var numc = 100; console.log("100 == 150: "+(numa == numb)); console.log("100 ! = 150: "+(numa ! = numb)); console.log("100 > 150: "+(numa > numb)); console.log("100 < 150: "+(numa < numb)); console.log("100 >= 150: "+(numa >= numb)); console.log("100 < 150: "+(numa <= numb)); var v1 = 100; var v2 = '100'; var v3 = 100; console.log("v1 == v2: " +(v1 == v2)); console.log("v1 === v2: " +(v1 === v2)); console.log("v1 === v3: " +(v1 === v3)); </script> </head> <body> </body> </html>Copy the code

Effect screenshot:

Logical operator

Logical operators are used to determine logic between variables or values.

Given x=6 and y=3, the following table explains the logical operators: ↓

The operator describe example
&& and (x < 10 && y> 1) is true
|| or (x = = 5 | | y = = 5) to false
! not ! (x = = y) to true

Sample code:

<! DOCTYPE html> <htm<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> console.log( "true && true : "+(true && true)); console.log( "true && false : "+(true && false)) ; console.log( "false && false : "+(false && false)); console.log( "true || true : "+(true || true)) ; console.log( "true || false : "+(true || false)) ; console.log( "false || false : "+(false || false)) ; console.log( "! true : "+(! true)) console.log( "! false : "+(! false)) var age = 70; var info = age>=18 ? 2. 'Of age' : 'of age '; console.log(info) </script> </head> <body> </body> </html>Copy the code

Effect screenshot:

This type has only two values, true and false for true or false. Often used to judge. True: the value can be true, a non-0 digit, or a non-empty character string. False: the value can be false, 0, an empty character string, null, or undefined

There is only one value, undefined. If a variable is declared using var and no value is initialized, the value of the variable is undefined.

A Null type is considered a Null object pointer, and a Null object reference.

Typeof operator, which returns the variable’s type name (string)

Sample code:

<! DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> var t1 = 'Keafmd'; Var t2 = 3.14; var t3 = false; var t4 = null; var t5 = undefined; var t6 = []; var t7 = {}; var t8 = function(){}; Console. log(" Typeof (string): "+typeof(T1)) console.log("typeof(number): "+typeof(T2)) console.log("typeof(null): "+typeof(null)) console.log("typeof(undefined): "+typeof(undefined)) console.log("typeof([]): "+typeof(t6)) console.log("typeof({}): "+typeof(t7)) console.log("typeof(function): "+ typeof (t8)) if (typeof (t1) = = = 'string') {the console. The log (' string ')} < / script > < title > < / title > < / head > < body > < / body > </html>Copy the code

Effect screenshot:

Writing is not easy, read if it helps you, thank you for your support!

If you are a computer terminal, see the lower right corner of the “one button three links”, yes click it [ha ha]


Come on!

Work together!

Keafmd