Javascript
Javascript
• \n- newline
• \t – horizontal tab
• \r –carriage return
• \\- back slash
• \’ – single quotes
• \” – double quotes
• No need to create new operator for document
or window object because these objects are
created by browsers
• Date object is JS built-in object
Precedence and Associativity
Decision making
• if ( studentGrade >= 60 )
document.writeln( "Passed" );
else
document.writeln( "Failed" );
• document.writeln( studentGrade >= 60 ? "Passed" :
"Failed" );
• if ( grade >= 90 )
document.writeln( "A" );
else if ( grade >= 80 )
document.writeln( "B" );
else if ( grade >= 70 )
document.writeln( "C" );
else if ( grade >= 60 )
document.writeln( "D" );
else
document.writeln( "F" );
Dangling-else problem
• if ( x > 5 )
if ( y > 5 )
document.writeln( "x and y are > 5" );
else
document.writeln( "x is <= 5" );
• if ( x > 5 )
if ( y > 5 )
document.writeln( "x and y are > 5" );
else
document.writeln( "x is <= 5" );
Nested if
• if ( x > 5 )
{
if ( y > 5 )
document.writeln( "x and y are > 5" );
}
else
document.writeln( "x is <= 5" );
while
Sentinel controlled loop
INCREMENT AND DECREMENT
OPERATOR
BREAK AND CONTINUE
MATH BUIT-IN OBJECTS
STRING OBJECTS
DATE OBJECTS
DOCUMENT- METHODS OR
PROPERTIES
REGULAR EXPRESSION
<p id="demo"></p>
<script>
let text = "Is this all there is?";
let pattern = /is/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
EXEC()
<!DOCTYPE html>
<html> • JavaScript
<body>
<h2>JavaScript Regular Expressions</h2>
Regular
<p>Do a global search for "is" in a string:</p> Expressions
<p id="demo"></p>
<p id="demo1"></p> • Do a global
<h1 id="r5">cse</h1>
<script>
search for
let text = "Is this all there is?";
let pattern = /is/g;
"is" in a
let result = pattern.exec(text); string:
let result1 = pattern.exec(text);
let result2 = pattern.exec(text); • is
document.getElementById("demo").innerHTML = result;
document.getElementById("demo1").innerHTML = result1; • is
document.getElementById("r5").innerHTML = result2;
</script>
</body>
</html>
TEST()
<p id="demo"></p>
<script>
let text = "The best things in life are free";
let pattern = /e/;
let result = pattern.test(text);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<!DOCTYPE html> • JavaScript Regular Expression
<html> • Using [ ] to find a range of characters
<body> • A global search for the character "h":
<h1>JavaScript Regular Expression</h1> • h,h
<h2>Using [ ] to find a range of characters</h2>
<p>A global search for the character "h":</p> • I,s, ,t,i,s, ,a,l,l, ,t,e,r,e, ,i,s,?
<p id="demo"></p>
<script>
let text = "Is this all there is?";
let pattern = /[h]/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<script>
let text = "123456789";
let pattern = /[1-4]/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
• let pattern = /[^1-4]/g;
<!DOCTYPE html> • JavaScript Regular Expression
<html> • The + Quantifier
<body> • A global search for at least one "o" in a string:
<h1>JavaScript Regular Expression</h1> • ooo,o,o,oo
<h2>The + Quantifier</h2>
<script>
let text = "Hellooo World! Hello W3Schools!";
let pattern = /o+/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<p id="demo"></p>
<script>
let text = "1, 100 or 1000?";
let pattern = /10?/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
<!DOCTYPE html> • JavaScript Regular Expression
<html> • The {} Quantifier
<body> • A global search for sequence of four digits:
<h1>JavaScript Regular Expression</h1> • 1000,1000
<h2>The {} Quantifier</h2>
<p id="demo"></p>
<script>
let text = "100, 1000 or 10000?";
let pattern = /\d{4}/g;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
• let pattern = /\d{3,}/g; // ATLEAST 3
• let pattern = /\d{3,4}/g; //substring 3 to 4 digits
• A search for "is" at the end of a string:
let text = "Is this his";
let pattern = /is$/;
• A global search for "Is" at the beginning of a string:
let text = "Is this his";
let pattern = /^Is/g;
• A search for "is" followed by " all":
• let text = "Is this all there is";
let pattern = /is(?= all)/g;
• Do a global, case insensitive search for "is" not followed by " all":
• let text = "Is this all there is";
let pattern = /is(?! all)/gi;
•