0% found this document useful (0 votes)
1 views29 pages

05_Javascript_ String, Numbers and Arrays

Uploaded by

dokipkip.apple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views29 pages

05_Javascript_ String, Numbers and Arrays

Uploaded by

dokipkip.apple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Javascript

String, Numbers, BigInt, Array


Strings
• JavaScript strings are for storing and manipulating text.

• To find the length of a string, use the built-in length property.

2
Escape Character
• Because strings must be written within quotes, JavaScript will misunderstand this string:

• The string will be chopped to "We are the so-called ".


• The solution to avoid this problem, is to use the backslash escape character.
• The backslash (\) escape character turns special characters into string characters:

3
Converting to Upper and Lower Case

JavaScript String concat()

4
JavaScript String indexOf() and lastIndexOf()

5
JavaScript String search()

JavaScript String includes()

6
JavaScript Template Strings
• Template Strings use back-ticks (``) rather than the quotes ("") to
define a string:
Eg. let text = `Hello world!`;

• Template Strings allow both single and double quotes inside a string:
Eg. let text = `He's often called "Johnny"`;

7
Interpolation
• Template Strings allow variables in strings.
• Template strings provide an easy way to interpolate variables in strings.
• Syntax - ${...}

• Template Strings allow interpolation of expressions in strings:

8
JavaScript Numbers

• JavaScript has only one type of number. Numbers can be written with or without
decimals.

Integer Precision

• Integers (numbers without a period or exponent notation) are accurate up to 15


digits.

9
JavaScript BigInt

• BigInt variables are used to store big integer values that are too big to be
represented by a normal JavaScript Numbers.

10
Array
• An array is a special variable, which can hold more than one value:

11
Accessing Array Elements

• You access an array element by referring to the index number:

12
Changing an Array Element
• This statement changes the value of the first element in cars:

Converting an Array to a String


• The JavaScript method toString() converts an array to a string of (comma
separated) array values.

13
JavaScript Array Methods
• The length property returns the length (size) of an array:

• The pop method removes the last element from an array:

• The push method adds a new element to an array (at the end):

14
JavaScript Array Methods

• The shift() method removes the first array element and "shifts" all other elements
to a lower index.

• The shift() method returns the value that was "shifted out":

15
JavaScript Array Methods

• The unshift() method adds a new element to an array (at the beginning), and
"unshifts" older elements:

• The unshift() method returns the new array length:

16
JavaScript Array Methods

• The concat() method creates a new array by merging (concatenating) existing


arrays:

• The concat() method can also take strings as arguments:

17
JavaScript Array Methods
• The splice() method can be used to add new items to an array:

• The first parameter (2) defines the position where new elements should be added (spliced
in).

• The second parameter (2) defines how many elements should be removed.

• The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
18
JavaScript Array Methods

• The splice() can also be used to remove elements without leaving "holes" in the
array:

• The slice() method slices out a piece of an array into a new array starting from 1.

• The method then selects elements from the start argument, and up to (but not
including) the end argument.

19
Sorting an Array
• The sort() method sorts an array alphabetically and the reverse() method reverses the
elements in an array.

• The sort () function sorts values as strings. So that, if numbers are sorted as strings, "25"
is bigger than "100", because "2" is bigger than "1". You can fix this by providing
a compare function:

20
Sorting an Array in Random Order

21
• Using Math.max() on an Array to find the highest number in an array:

HomeWork!

• Using Math.min() on an Array to find the lowest number in an array:

22
JavaScript Array Iteration

JavaScript Array forEach()

• The forEach() method calls a function (a callback function) once for each array
element.

(value, index, array)

23
JavaScript Array map()
• creates a new array by performing a function on each array element.

HomeWork!

JavaScript Array filter()


• creates a new array with array elements that pass a test.

24
JavaScript Array reduce()
• runs a function on each array element to produce (reduce it to) a single value.

• The reduce() method can accept an initial value:

25
JavaScript Array indexOf() method
• searches an array for an element value and returns its position.

JavaScript Array lastIndexOf() method


• returns the position of the last occurrence of the specified element.

26
JavaScript Array find()
• returns the value of the first array element that passes a test function.

JavaScript Array findIndex()


• returns the index of the first array element that passes a test function.

27
JavaScript Array includes()
• check if an element is present in an array

• it will return true if the element is present in the array and false if the
element is not present.

28
References
• https://www.w3schools.com
• Learn to Code — For Free — Coding Courses for Busy People (freecodecamp.org)
• JavaScript - Overview | Tutorialspoint

29

You might also like