0% found this document useful (0 votes)
11 views21 pages

02 JS

The document provides an overview of JavaScript objects, strings, numbers, and arrays, including their properties and methods. It explains how to create objects, manipulate strings, perform arithmetic with numbers, and utilize various array methods. Key concepts such as escaping characters, string manipulation methods, and array operations like push, pop, and sort are highlighted.

Uploaded by

fenen64815
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)
11 views21 pages

02 JS

The document provides an overview of JavaScript objects, strings, numbers, and arrays, including their properties and methods. It explains how to create objects, manipulate strings, perform arithmetic with numbers, and utilize various array methods. Key concepts such as escaping characters, string manipulation methods, and array operations like push, pop, and sort are highlighted.

Uploaded by

fenen64815
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/ 21

JavaScript

Objects

1
Js Objects
const person = {
name: "cumar", // property
age: 25, // property
getDetails: function () { // method
console.log('My name is ' + this.name;
}
}

2
Object types

3
Getting object types
const str = "hello"
const num = 10
const tf = true
const obj = { name: 'cali' }

console.log(typeof str) // string


console.log(typeof num) // number
console.log(typeof tf) // boolean
console.log(typeof obj) // object

4
Strings

5
Strings
• Zero or more characters written inside quotes.
– single or double quotes:
const name = "cali"
const name = 'axmed'
const sentence = "I'm student"

6
Escaping characters
• Single quote
const sentence = 'I \'m student'
• Single quote
const sentence = "I \"m student"
• Single quote
const sentence = 'I \\m student'

7
String methods
• Length: get length of string
const text = "I am coder"
const coder = text.length // 10

• Slice: extracts a part of a string [start, end] and


returns the extracted part
const text = "I am coder"
const coder = text.slice(5, 10) // coder

8
String methods
• Substring: similar to slice(). The difference is that the
second parameter specifies the length of the
extracted part.
const text = "I am coder"
const coder = text.substr(5, 4) // code

• Replace: replaces a specified value with another


value in a string
const text = "I am coder"
// I am driver
const coder = text.replace("coder", "driver")
9
String methods
• toUppercase: converting to upper case.
const text = "I am coder"
// I AM CODER
const coder = text.toUpperCase()

• toLowercase: converting to lower case.


const text = "I AM CODER"
// i am coder
const coder = text.toLowerCase()

10
String methods
• trim: removes whitespace from the [left, right] sides
of a string.
const text = " I am coder "
// I AM CODER
const coder = text.trim()

• toLowercase: converting to lower case.


const text = "I AM CODER"
// i am coder
const coder = text.toLowerCase()
11
String methods
• charAt: returns the character at a specified index
(position) in a string.
const text = "I am coder"
const coder = text.charAt(3) // m

• split: split a string into substrings using the specified


separator and return them as an array.
const text = "I AM CODER"
// ["I", "AM", "CODER"]
const coder = text.split(" ")
12
Numbers
• Floating point arithmetic is not always 100% accurate
(in any programming language).
const num = 0.1 + 0.2 // 0.30000000000000004
const num = (0.1 * 10 + 0.2 * 10) / 10 // 0.3

13
Number methods
• toString: returns a number as a string.
const figure = 10
console.log(figure.toString()) // "10"

• toFixed: returns a string, with the number written


with a specified number of decimals.
const figure = 10.5465
console.log(figure.toFixed(2)) // "10.55"

14
Number methods
• Number: used to convert [numeric] string variables
to numbers.
const figure = "10.5465"
console.log(Number(figure)) // 10.5465

• parseFloat: parses its argument and returns a


floating point number.
• parseInt: parses its argument and returns a whole
number

15
Arrays

16
Array methods
• Length: property returns the length (size) of an array.
const list = [1, 2, 3, 4]
list.length // 4

• Join: joins all array elements into a string.


const list = ["xasan", "cali", "maxamed"]
list.join(" ") // xasan cali maxamed

17
Array methods
• push: adds a new element to an array (at the end).
const list = [1, 2, 3, 4]
list.push(5) // [1, 2, 3, 4, 5]

• pop: removes an element from the array (at the


end).
const list = [1, 2, 3, 4]
list.pop() // [1, 2, 3, 4]

18
Array methods
• shift: removes the first array element and "shifts" all
other elements to a lower index.
const list = [1, 2, 3, 4]
list.shift() // [2, 3, 4]

• unshift: adds a new element to an array (at the


beginning), and "unshifts" older elements.
const list = [1, 2, 3, 4]
list.unshift(5) // [5, 1, 2, 3, 4]

19
Array methods
• sort: sorts an array alphabetically/numerically.
const list = ["xasan", "cali", "maxamed"]
list.sort() // ['cali', 'maxamed', 'xasan']

• reverse: reverses the elements in an array.


const list = ["xasan", "cali", "maxamed"]
list.reverse() // ['maxamed', 'cali',
'xasan']

20
Array methods
• indexOf: searches an array for an element value and
returns its position [first occurrence] otherwise -1.
const list = ["xasan", "cali", "maxamed"]
list.indexOf("cali") // 1

21

You might also like