SlideShare ist ein Scribd-Unternehmen logo
1 von 10
JavaScript Built-in objects 
JavaScript supports a number of built-in objects that extend the flexibility of the language. These objects 
are Date, Math, String, Array, and Object. 
1. String Objects : 
<html> 
<body> 
<p id="demo"></p> 
<script> 
var carName1 = "Volvo XC60"; 
var carName2 = 'Volvo XC60'; 
var answer1 = "It's alright"; 
var answer2 = "He is called 'Johnny'"; 
var answer3 = 'He is called "Johnny"'; 
document.getElementById("demo").innerHTML = 
carName1 + "<br>" + 
carName2 + "<br>" + 
answer1 + "<br>" + 
answer2 + "<br>" + 
answer3; 
</script></body></html> 
2. String Length: 
<html><body> 
<p id="demo"></p> 
<script> 
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
document.getElementById("demo").innerHTML = txt.length; 
</script></body></html> 
3. Strings can be Objects 
 Normally, JavaScript strings are primitive values, created from literals: var firstName = 
"John" 
 But strings can also be defined as objects with the keyword new: var firstName = new 
String("John") 
<html><body>
<p id="demo"></p> 
<script> 
var x = "John"; // x is a string 
var y = new String("John"); // y is an object 
document.getElementById("demo").innerHTML = typeof x + " " + typeof y; 
</script> 
</body></html> 
JavaScript string methods 
4. The slice() Method 
 Slice() extracts a part of a string and returns the extracted part in a new string. 
 The method takes 2 parameters: the starting index (position), and the ending index 
(position). 
 This example slices out a portion of a string from position 7 to position 13: 
<html><body> 
<p>The slice() method extract a part of a string and returns the extracted parts in a new 
string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.slice(7,13); 
</script></body></html> 
If a parameter is negative, the position is counted from the end of the string. 
5. This example slices out a portion of a string from position -12 to position -6: 
<html><body> 
<p>The slice() method extract a part of a string and returns the extracted parts in a new 
string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.slice(-12,-6); 
</script></body></html>
6.If you omit the second parameter, the method will slice out the rest of the sting: 
<html><body> 
<p>The slice() method extract a part of a string and returns the extracted parts in a new 
string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.slice(7); 
</script> 
</body></html> 
7. or, counting from the end : 
<html><body> 
<p>The slice() method extract a part of a string and returns the extracted parts in a new 
string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.slice(-12); 
</script></body></html> 
8. The substring() Method 
 Substring () is similar to slice (). 
 The difference is that substring () cannot accept negative indexes. 
<html><body> 
<p>The substr() method extract a part of a string and returns the extracted parts in a 
new string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.substring(7,13); 
</script></body></html>
9. The substr() Method 
 substr() is similar to slice(). 
 The difference is that the second parameter specifies the length of the extracted part. 
<html><body> 
<p>The substr() method extract a part of a string and returns the extracted parts in a 
new string:</p> 
<p id="demo"></p> 
<script> 
var str = "Apple, Banana, Kiwi"; 
document.getElementById("demo").innerHTML = str.substr(7,6); 
</script></body></html> 
 If the first parameter is negative, the position counts from the end of the string. 
 The second parameter can not be negative, because it defines the length. 
 If you omit the second parameter, substr() will slice out the rest of the string. 
10. Replacing String Content 
 The replace() method replaces a specified value with another value in a string: 
<html><body> 
<p>Replace "Microsoft" with "W3Schools" in the paragraph below:</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo">Please visit Microsoft!</p> 
<script> 
function myFunction() { 
var str = document.getElementById("demo").innerHTML; 
var txt = str.replace("Microsoft","W3Schools"); 
document.getElementById("demo").innerHTML = txt; 
} 
</script></body></html> 
11. Converting to Upper and Lower Case 
A string is converted to upper case with toUpperCase(): 
<html><body>
<p>Convert string to upper case:</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo">Hello World!</p> 
<script> 
function myFunction() { 
var text = document.getElementById("demo").innerHTML; 
document.getElementById("demo").innerHTML = text.toUpperCase(); 
} 
</script></body></html> 
A string is converted to lower case with toLowerCase(): 
12. The concat() Method 
 concat() joins two or more strings: 
<html><body> 
<p>The concat() method joins two or more strings:</p> 
<p id="demo"></p> 
<script> 
var text1 = "Hello"; 
var text2 = "World!" 
document.getElementById("demo").innerHTML = text1.concat(" ",text2); 
</script></body></html> 
 The concat() method can be used instead of the plus operator. These two lines do the same: 
var text = "Hello" + " " + "World!"; 
var text = "Hello".concat(" ","World!"); 
13. Extracting String Characters 
· charAt(position) 
The charAt() Method 
 The charAt() method returns the character at a specified index (position) in a string: 
<html><body> 
<p>The charAt() method returns the character at a given position in a string:</p> 
<p id="demo"></p>
<script> 
var str = "HELLO WORLD"; 
document.getElementById("demo").innerHTML = str.charAt(0); 
</script></body></html> 
Date Objects: 
14. Displaying Dates 
<html><body> 
<p id="demo"></p> 
<script> 
document.getElementById("demo").innerHTML = Date(); 
</script> 
</body></html> 
 A date consists of a year, a month, a week, a day, a minute, a second, and a millisecond. 
 Date objects are created with the new Date() constructor. 
There are 4 ways of initiating a date: 
i. new Date() 
ii. new Date(milliseconds) 
iii. new Date(dateString) 
iv. new Date(year, month, day, hours, minutes, seconds, milliseconds) 
15. Using new Date (), without parameters, creates a new date object with the current date 
and time: 
<html><body> 
<p id="demo"></p> 
<script> 
var d = new Date(); 
document.getElementById("demo").innerHTML = d; 
</script></body></html> 
16. Using new Date (), with a date string, creates a new date object with the specified date 
and time: 
<html><body>
<p id="demo"></p> 
<script> 
var d = new Date("October 13, 2014 11:13:00"); 
document.getElementById("demo").innerHTML = d; 
</script></body></html> 
17. Using new Date(), with 7 numbers, creates a new date object with the 
specified date and time: The 7 numbers specify the year, month, day, hour, minute, 
second, and millisecond, in that order: 
<html><body> 
<p id="demo"></p> 
<script> 
var d = new Date(99,5,24,11,33,30,0); 
document.getElementById("demo").innerHTML = d; 
</script></body></html> 
18. Math Functions: 
 The Math object allows you to perform mathematical tasks. 
 One common use of the Math object is to create a random number: 
<html><body> 
<p>Math.random() returns a random number betwween 0 and 1.</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = Math.random(); 
} 
</script></body></html> 
19. Math.min() and Math.max() 
Math.min() can be used to find the lowest value in a list of arguments. 
<html><body> 
<p>Math.min() returns the lowest value.</p> 
<button onclick="myFunction()">Try it</button>
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = 
Math.min(0, 150, 30, 20, -8); 
} 
</script></body></html> 
20. Math.max() can be used to find the highest value in a list of arguments. 
<html> 
<body> 
<p>Math.max() returns the higest value.</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = 
Math.max(0, 150, 30, 20, -8); 
} 
</script></body></html> 
21. Math.round(): rounds a number to the nearest 
integer 
<html><body> 
<p>Math.round() rounds a number to its nearest integer.</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = Math.round(4.4); 
} 
</script></body></html> 
22. Math.floor(): rounds a number down to the nearest 
integer: 
<html><body> 
<p>Math.floor() rounds a number <strong>down</strong> to its nearest integer.</p>
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = Math.floor(4.7); 
} 
</script></body></html> 
23. Math Constants 
JavaScript provides 3 mathematical constants that can be accessed with the Math object: 
<html> 
<body> 
<p>Math constants are E, PI, SQR2, SQR1_2, LN2, LN10, LOG2E, LOG10E</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = 
Math.E + "<br>" + 
Math.PI + "<br>" + 
Math.SQRT2 + "<br>" ; 
} 
</script></body></html>
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = Math.floor(4.7); 
} 
</script></body></html> 
23. Math Constants 
JavaScript provides 3 mathematical constants that can be accessed with the Math object: 
<html> 
<body> 
<p>Math constants are E, PI, SQR2, SQR1_2, LN2, LN10, LOG2E, LOG10E</p> 
<button onclick="myFunction()">Try it</button> 
<p id="demo"></p> 
<script> 
function myFunction() { 
document.getElementById("demo").innerHTML = 
Math.E + "<br>" + 
Math.PI + "<br>" + 
Math.SQRT2 + "<br>" ; 
} 
</script></body></html>

Weitere ähnliche Inhalte

Was ist angesagt?

mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomer
zefhemel
 

Was ist angesagt? (20)

Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deployments
 
XML-RPC vs Psycopg2
XML-RPC vs Psycopg2XML-RPC vs Psycopg2
XML-RPC vs Psycopg2
 
How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF How to Bring Common UI Patterns to ADF
How to Bring Common UI Patterns to ADF
 
Config BuildConfig
Config BuildConfigConfig BuildConfig
Config BuildConfig
 
Ip project
Ip projectIp project
Ip project
 
Telephone billing system in c++
Telephone billing system in c++Telephone billing system in c++
Telephone billing system in c++
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
 
My java file
My java fileMy java file
My java file
 
Serverless Functions and Vue.js
Serverless Functions and Vue.jsServerless Functions and Vue.js
Serverless Functions and Vue.js
 
mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomer
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
Surfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSurfacing External Data Through Magnolia
Surfacing External Data Through Magnolia
 
Angular mix chrisnoring
Angular mix chrisnoringAngular mix chrisnoring
Angular mix chrisnoring
 
Idoc script beginner guide
Idoc script beginner guide Idoc script beginner guide
Idoc script beginner guide
 
Rooted 2010 ppp
Rooted 2010 pppRooted 2010 ppp
Rooted 2010 ppp
 
Week 12 code
Week 12 codeWeek 12 code
Week 12 code
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
MaintainStaffTable
MaintainStaffTableMaintainStaffTable
MaintainStaffTable
 
Spring 2.5
Spring 2.5Spring 2.5
Spring 2.5
 

Ähnlich wie 14922 java script built (1)

Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
Hitesh Patel
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
ikirkton
 
Java script frame window
Java script frame windowJava script frame window
Java script frame window
H K
 

Ähnlich wie 14922 java script built (1) (20)

JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
FYBSC IT Web Programming Unit III Core Javascript
FYBSC IT Web Programming Unit III  Core JavascriptFYBSC IT Web Programming Unit III  Core Javascript
FYBSC IT Web Programming Unit III Core Javascript
 
FYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III JavascriptFYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III Javascript
 
Java Script
Java ScriptJava Script
Java Script
 
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
 
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
 
Javascript 1
Javascript 1Javascript 1
Javascript 1
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
 
JavaScript Training
JavaScript TrainingJavaScript Training
JavaScript Training
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
TYCS Ajax practicals sem VI
TYCS Ajax practicals sem VI TYCS Ajax practicals sem VI
TYCS Ajax practicals sem VI
 
JavaScript lesson 1.pptx
JavaScript lesson 1.pptxJavaScript lesson 1.pptx
JavaScript lesson 1.pptx
 
Experience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - HighlightsExperience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - Highlights
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
Polymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentsPolymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web components
 
28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScript
 
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docxbbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
bbyopenApp_Code.DS_StorebbyopenApp_CodeVBCodeGoogleMaps.docx
 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
 
Java script frame window
Java script frame windowJava script frame window
Java script frame window
 
단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발단일 페이지 인터페이스 웹/앱 개발
단일 페이지 인터페이스 웹/앱 개발
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

14922 java script built (1)

  • 1. JavaScript Built-in objects JavaScript supports a number of built-in objects that extend the flexibility of the language. These objects are Date, Math, String, Array, and Object. 1. String Objects : <html> <body> <p id="demo"></p> <script> var carName1 = "Volvo XC60"; var carName2 = 'Volvo XC60'; var answer1 = "It's alright"; var answer2 = "He is called 'Johnny'"; var answer3 = 'He is called "Johnny"'; document.getElementById("demo").innerHTML = carName1 + "<br>" + carName2 + "<br>" + answer1 + "<br>" + answer2 + "<br>" + answer3; </script></body></html> 2. String Length: <html><body> <p id="demo"></p> <script> var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.getElementById("demo").innerHTML = txt.length; </script></body></html> 3. Strings can be Objects  Normally, JavaScript strings are primitive values, created from literals: var firstName = "John"  But strings can also be defined as objects with the keyword new: var firstName = new String("John") <html><body>
  • 2. <p id="demo"></p> <script> var x = "John"; // x is a string var y = new String("John"); // y is an object document.getElementById("demo").innerHTML = typeof x + " " + typeof y; </script> </body></html> JavaScript string methods 4. The slice() Method  Slice() extracts a part of a string and returns the extracted part in a new string.  The method takes 2 parameters: the starting index (position), and the ending index (position).  This example slices out a portion of a string from position 7 to position 13: <html><body> <p>The slice() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.slice(7,13); </script></body></html> If a parameter is negative, the position is counted from the end of the string. 5. This example slices out a portion of a string from position -12 to position -6: <html><body> <p>The slice() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.slice(-12,-6); </script></body></html>
  • 3. 6.If you omit the second parameter, the method will slice out the rest of the sting: <html><body> <p>The slice() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.slice(7); </script> </body></html> 7. or, counting from the end : <html><body> <p>The slice() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.slice(-12); </script></body></html> 8. The substring() Method  Substring () is similar to slice ().  The difference is that substring () cannot accept negative indexes. <html><body> <p>The substr() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.substring(7,13); </script></body></html>
  • 4. 9. The substr() Method  substr() is similar to slice().  The difference is that the second parameter specifies the length of the extracted part. <html><body> <p>The substr() method extract a part of a string and returns the extracted parts in a new string:</p> <p id="demo"></p> <script> var str = "Apple, Banana, Kiwi"; document.getElementById("demo").innerHTML = str.substr(7,6); </script></body></html>  If the first parameter is negative, the position counts from the end of the string.  The second parameter can not be negative, because it defines the length.  If you omit the second parameter, substr() will slice out the rest of the string. 10. Replacing String Content  The replace() method replaces a specified value with another value in a string: <html><body> <p>Replace "Microsoft" with "W3Schools" in the paragraph below:</p> <button onclick="myFunction()">Try it</button> <p id="demo">Please visit Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("Microsoft","W3Schools"); document.getElementById("demo").innerHTML = txt; } </script></body></html> 11. Converting to Upper and Lower Case A string is converted to upper case with toUpperCase(): <html><body>
  • 5. <p>Convert string to upper case:</p> <button onclick="myFunction()">Try it</button> <p id="demo">Hello World!</p> <script> function myFunction() { var text = document.getElementById("demo").innerHTML; document.getElementById("demo").innerHTML = text.toUpperCase(); } </script></body></html> A string is converted to lower case with toLowerCase(): 12. The concat() Method  concat() joins two or more strings: <html><body> <p>The concat() method joins two or more strings:</p> <p id="demo"></p> <script> var text1 = "Hello"; var text2 = "World!" document.getElementById("demo").innerHTML = text1.concat(" ",text2); </script></body></html>  The concat() method can be used instead of the plus operator. These two lines do the same: var text = "Hello" + " " + "World!"; var text = "Hello".concat(" ","World!"); 13. Extracting String Characters · charAt(position) The charAt() Method  The charAt() method returns the character at a specified index (position) in a string: <html><body> <p>The charAt() method returns the character at a given position in a string:</p> <p id="demo"></p>
  • 6. <script> var str = "HELLO WORLD"; document.getElementById("demo").innerHTML = str.charAt(0); </script></body></html> Date Objects: 14. Displaying Dates <html><body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = Date(); </script> </body></html>  A date consists of a year, a month, a week, a day, a minute, a second, and a millisecond.  Date objects are created with the new Date() constructor. There are 4 ways of initiating a date: i. new Date() ii. new Date(milliseconds) iii. new Date(dateString) iv. new Date(year, month, day, hours, minutes, seconds, milliseconds) 15. Using new Date (), without parameters, creates a new date object with the current date and time: <html><body> <p id="demo"></p> <script> var d = new Date(); document.getElementById("demo").innerHTML = d; </script></body></html> 16. Using new Date (), with a date string, creates a new date object with the specified date and time: <html><body>
  • 7. <p id="demo"></p> <script> var d = new Date("October 13, 2014 11:13:00"); document.getElementById("demo").innerHTML = d; </script></body></html> 17. Using new Date(), with 7 numbers, creates a new date object with the specified date and time: The 7 numbers specify the year, month, day, hour, minute, second, and millisecond, in that order: <html><body> <p id="demo"></p> <script> var d = new Date(99,5,24,11,33,30,0); document.getElementById("demo").innerHTML = d; </script></body></html> 18. Math Functions:  The Math object allows you to perform mathematical tasks.  One common use of the Math object is to create a random number: <html><body> <p>Math.random() returns a random number betwween 0 and 1.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.random(); } </script></body></html> 19. Math.min() and Math.max() Math.min() can be used to find the lowest value in a list of arguments. <html><body> <p>Math.min() returns the lowest value.</p> <button onclick="myFunction()">Try it</button>
  • 8. <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.min(0, 150, 30, 20, -8); } </script></body></html> 20. Math.max() can be used to find the highest value in a list of arguments. <html> <body> <p>Math.max() returns the higest value.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.max(0, 150, 30, 20, -8); } </script></body></html> 21. Math.round(): rounds a number to the nearest integer <html><body> <p>Math.round() rounds a number to its nearest integer.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.round(4.4); } </script></body></html> 22. Math.floor(): rounds a number down to the nearest integer: <html><body> <p>Math.floor() rounds a number <strong>down</strong> to its nearest integer.</p>
  • 9. <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.floor(4.7); } </script></body></html> 23. Math Constants JavaScript provides 3 mathematical constants that can be accessed with the Math object: <html> <body> <p>Math constants are E, PI, SQR2, SQR1_2, LN2, LN10, LOG2E, LOG10E</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.E + "<br>" + Math.PI + "<br>" + Math.SQRT2 + "<br>" ; } </script></body></html>
  • 10. <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.floor(4.7); } </script></body></html> 23. Math Constants JavaScript provides 3 mathematical constants that can be accessed with the Math object: <html> <body> <p>Math constants are E, PI, SQR2, SQR1_2, LN2, LN10, LOG2E, LOG10E</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.E + "<br>" + Math.PI + "<br>" + Math.SQRT2 + "<br>" ; } </script></body></html>