Web Tech 2
Web Tech 2
The core is the heart of the language, including its operators, expressions,
statements, and subprograms
Apart from these, JavaScript also has some predefined words, such as alert,
open, java, and self.
Variables in Javascript
● A variable is a name associated with a piece of data
● Variable = information container
● In Javascript (Js), Variable definition starts with ‘var’, ‘let’, or ‘const’
● Js is dynamically typed language
● Js is case sensitive
var x = 10;
var y = 17;
var colour = “red”;
var name = “Katie”;
Different ways of declaring a variable
JavaScript uses the keywords var, let and const to declare variables.
let v1=“hi”;
console.log (v1);
hi
Challenege-1: The Age Calculator
• Store your birth year in a variable.
• Store a future year in a variable.
• Calculate your age for that year based on the stored values.
• Output them to the screen like: “The age is NN", substituting the values.
Solution
let birthyear=1988;
let futureyear=2024;
let age = futureyear – birthyear;
console.log(The age is ‘+age’);
Javascript Primitives
JavaScript has five primitive types
Undefined
Numbers
Primitive Null
Strings
Booleans
Datatypes: Undefined output
<script>
var myvar;
alert(myvar);
</script>
output
<script>
var myvar= 10;
alert(myvar);
</script>
Datatypes: Null
output
<script>
var myvar= null;
alert(myvar);
</script>
Datatypes: Numbers
<script> output
var num1,num2;
var sum;
num1 = 10;
num2 = 20;
sum = num1 + num2;
alert(sum);
</script>
Datatypes: Strings
<script> output
var str1,str2,str3;
str1 = "welcome to ";
str2 = 'javascript';
str3 = str1+str2;
alert(str3);
</script>
Datatypes: Booleans
<script> Output
var course_completed;
course_completed = false;
course_completed = true;
alert('course completed:'+course_completed);
<script>
Datatypes: Static vs Dynamic
Static type
/* C code */
int num, sum; // explicit declaration
num = 5; // now use the variables
sum = 10;
sum = sum + num;
Dynamic type
/* python code */
num = 10 // directly using the variable
num = “my number”
Datatypes: Static vs Dynamic
else if Statement:
Conditional Statements
switch Statement:
Looping statements
for Loop:
while Loop:
do-while Loop:
Looping statements
for...in Loop (for iterating over object properties)
continue Statement:
Skips the rest of the code inside a loop for the current iteration and
continues with the next iteration.
return Statement:
Exits a function and optionally returns a value.
Challenge-4
Write a JavaScript program to check whether three given integer values are in
the range 50..99 (inclusive). Return true if one or more of them are in the
specified range.
Objects in Javascript
Object is a collection of related properties and methods.
Properties represents the features of the object
Method represents actions that are performed on objects
Object creation: var my_object = new Object(); var my_car = new Object();
my_car.make = "Ford";
my_car.model = "Fusion";
objectName.propertyName
or
objectName["propertyName"]
Objects in Javascript
var my_car = {make:“Ford",model:“Fusion"};
delete my_car.model;
Js has a loop statement, for-in, that is perfect for listing the properties of an
object.
Has methods for the trigonometric functions, such as sin (for sine) and cos
(for cosine)
All of the Math methods are referenced through the Math object, as in
Math.sin(x).
Date Object
This object is used for obtaining date and time (based on computer’s local
time)
String Object
Used for many string related functionalities (such as concatenation)
var x = 5 + 5; x 10
var y = "5" + 5; y 55
JavaScript will attempt to convert one or both of the values to a common type
before completing the operation
Type Conversions
Explicit type conversions: Type casting
Numeric Conversion:
Number(value) - Converts the value to a number
parseInt(string) - Parses a string and returns an integer
parseFloat(string) -Parses a string and returns a floating-point number
Boolean Conversion:
Boolean(value) - Converts the value to a boolean
String Properties and Methods
Screen Output and Keyboard Input
1. Console.log: Used to print messages along with the values of the
variables to the console. [console.log(variable_name);]
2. document.write: Used for displaying the message on the web browser.
[document.write(“Hello World”);]
3. Alert box: A pop up box with some message will be displayed.
[alert(“Hello world”);]
Screen Output and Keyboard Input
4. Confirm box: A message about confirmation will be displayed.
[confirm("Do you want to continue this download?");]
Screen Output and Keyboard Input
5. Prompt box: A prompt will be opened for user to enter the input
[prompt("What is your name?", "");]
Arrays
Arrays in JavaScript are a type of object that allows you to store and
manipulate a collection of elements.
Arrays
1. Creating an array:
2. Accessing Elements:
fruits[1] = "Grapes";
console.log(fruits); // Output: ["Apple", "Grapes", "Orange"]
4. Array Methods:
Push and Unshift: Push method adds new items to the end of an array
whereas Unshift method adds new items at front of an array.
array.push(item1, item2, ..., itemX);
array.unshift(item1, item2, ..., itemX);
Arrays
Pop and Shift: Pop removes the last item of an array and slice removes the
first item of an array
array.pop()
array.shift()
1. Function Declaration:
Functions
2. Function parameters:
Functions can take parameters, which are values that you provide when
calling the function.
Functions
3. Return statement:
Variables declared inside a function are typically scoped to that function and
not accessible outside of it.
Challenge-8.1
1. Write a javascript program for Calculator Functions.
Each function should take two parameters (two numbers) and return the
result of the corresponding operation.
test() is case-sensitive by
default
Pattern matching using regular expressions
Matching and Extracting:
Use match method to find matches in a string.
Returns an array of matches or null if no match found.
Flag Description
/\D\d\D/
/\w\w\w/
/x*y+z?/
/[A-Za-z]\w*/