SlideShare a Scribd company logo
Learn JavaScript
Mahmoud Asadi
Comments
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
var x = 5; // Declare x, give it the value of 5
var y = x + 2; // Declare y, give it the value of x + 2
Variables
var x = 5;
var y = 6;
var z = x + y;
Data Types
var x; // Now x is undefined
var x; // Now x is defined
var x = 5; // Now x is a Number
var x = "John"; // Now x is a String
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
Data Types
var x = true; // boolean type
var y = false;
var cars = ["Saab", "Volvo", "BMW"]; // array
var person = {firstName:"John", lastName:"Doe", age:50 }; // Object
var cars; // Value is undefined
var person = null; // Value is null
The typeof Operator
typeof "John" // Returns string
typeof 3.14 // Returns number
typeof false // Returns boolean
typeof [1,2,3,4] // Returns object
typeof {name:'John', age:34} // Returns object
Functions
Syntax:
functionName(parameter1, parameter2, parameter3) {
code to be executed
}
var x = myFunction(4, 3); // Function is called, return value will end up in x
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
Why Functions?
• You can reuse code: Define the code once, and use it
many times.
• You can use the same code many times with different
arguments, to produce different results.
Functions Used as Variables
Example
Instead of:
temp = toCelsius(32);
text = "The temperature is " + temp + " Centigrade";
You can use:
text = "The temperature is " + toCelsius(32) + " Centigrade";
JavaScript Objects
• In real life, a car is an object.
Properties Methods
car.name = Fiat
car.model = 500
car.weight = 850kg
car.color = white
car.start()
car.drive()
car.brake()
car.stop()
JavaScript Objects
• Objects are variables too. But objects can contain many values.
• This code assigns many values (Fiat, 500, white) to a variable named car:
• var car = {type:"Fiat", model:500, color:"white"};
• objectName.propertyName
• objectName.methodName()
JavaScript Math Object
• The Math object allows you to perform mathematical tasks on numbers.
• Math.random(); // returns a random number between 0 and 1
• Math.min(0, 150, 30, 20, -8); // returns -8
• Math.max(0, 150, 30, 20, -8); // returns 150
• Math.round(4.7); // returns 5 (rounds a number to the nearest integer)
• Math.floor(4.7); // returns 4 (rounds a number down to the nearest integer)
• Math.floor(Math.random() * 2); // returns a random number between 0 and 1
JavaScript Arrays
• var array-name = [item1, item2, ...];
• var cars = ["Saab", "Volvo", "BMW"];
• var name = cars[0];
• cars[0] = "Opel";
Adding Array Elements
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits
Looping Array Elements
• var index;
var fruits = ["Banana", "Orange", "Apple", "Mango"];
for (index = 0; index < fruits.length; index++) {
text += fruits[index];
}
JavaScript Array Methods
• Converting Arrays to Strings
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
• fruits.valueOf();
• fruits.toString();
JavaScript Array Methods
• The join() method also joins all array elements into a string.
• var fruits = ["Banana", "Orange","Apple", "Mango"];
• fruits.join(" * ");
Popping and Pushing
• The pop() method removes the last element from an array:
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // Removes the last element ("Mango") from fruits
• The push() method adds a new element to an array (at the end):
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits
Shifting Elements
• The shift() method removes the first element of an array, and "shifts" all
other elements one place down.
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); // Removes the first element "Banana" from fruits
• The unshift() method adds a new element to an array (at the beginning), and
"unshifts" older elements:
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon"); // Adds a new element "Lemon" to fruits
Deleting Elements
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0]; // Changes the first element in fruits to undefined
Sorting an Array
• The sort() method sorts an array alphabetically:
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
• The reverse() method reverses the elements in an array.
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
fruits.reverse(); // Reverses the order of the elements
JavaScript Booleans
• Very often, in programming, you will need a data type that can only have one
of two values, like
• YES / NO
• ON / OFF
• TRUE / FALSE
• For this, JavaScript has a Boolean data type. It can only take the values true
or false.
The Boolean() Function
• Boolean(10 > 9) // returns true
• (10 > 9) // also returns true
• 10 > 9 // also returns true
Operator Description Example
== equal to if (day == "Monday")
> greater than if (salary > 9000)
< less than if (age < 18)
The End
Good luck !

More Related Content

PPTX
JavaScript Introduction and Implementation
ODP
Drupal Best Practices
PPT
A Short Introduction To jQuery
PDF
Working with Javascript in Rails
PPTX
jQuery PPT
PPTX
PHP Basics
PPTX
jQuery
TXT
Change log
JavaScript Introduction and Implementation
Drupal Best Practices
A Short Introduction To jQuery
Working with Javascript in Rails
jQuery PPT
PHP Basics
jQuery
Change log

What's hot (20)

ODP
Introduction to jQuery
PDF
jQuery in 15 minutes
PPT
JQuery introduction
PDF
Aniki has come
PDF
Image manipulation in WordPress 3.5
PDF
Learning jQuery in 30 minutes
PPT
jQuery Performance Rules
PPTX
Jquery introduction
PDF
Dropdown List in PHP
PPTX
jQuery from the very beginning
PDF
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
PDF
Prototype & jQuery
PPTX
Php introduction
PPT
JQuery: Introduction
PDF
jQuery Essentials
ZIP
Learning the basics of the Drupal API
PPTX
JavaScript and jQuery Basics
PDF
jQuery - Chapter 5 - Ajax
PDF
Crowdsourcing with Django
PPTX
jQuery Presentation
Introduction to jQuery
jQuery in 15 minutes
JQuery introduction
Aniki has come
Image manipulation in WordPress 3.5
Learning jQuery in 30 minutes
jQuery Performance Rules
Jquery introduction
Dropdown List in PHP
jQuery from the very beginning
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Prototype & jQuery
Php introduction
JQuery: Introduction
jQuery Essentials
Learning the basics of the Drupal API
JavaScript and jQuery Basics
jQuery - Chapter 5 - Ajax
Crowdsourcing with Django
jQuery Presentation
Ad

Similar to Learn java script (20)

PPTX
JavaScript.pptx
PPTX
1-JAVA SCRIPT. servere-side applications vs client side applications
PPT
Javascript
PPSX
Javascript variables and datatypes
PPTX
Javascript 101
PDF
Fewd week5 slides
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
PPTX
An introduction to javascript
PPT
Java Script ppt
PPTX
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
PPTX
gdscWorkShopJavascriptintroductions.pptx
PPTX
Front end fundamentals session 1: javascript core
PPTX
03. Week 03.pptx
PPT
data-types-operators-datatypes-operators.ppt
PPTX
Javascript
PPTX
An Introduction to JavaScript
PPT
An introduction to javascript
PPTX
introduction to java scriptsfor sym.pptx
PDF
JavaScript and jQuery - Web Technologies (1019888BNR)
PPTX
CSC PPT 13.pptx
JavaScript.pptx
1-JAVA SCRIPT. servere-side applications vs client side applications
Javascript
Javascript variables and datatypes
Javascript 101
Fewd week5 slides
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
An introduction to javascript
Java Script ppt
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
gdscWorkShopJavascriptintroductions.pptx
Front end fundamentals session 1: javascript core
03. Week 03.pptx
data-types-operators-datatypes-operators.ppt
Javascript
An Introduction to JavaScript
An introduction to javascript
introduction to java scriptsfor sym.pptx
JavaScript and jQuery - Web Technologies (1019888BNR)
CSC PPT 13.pptx
Ad

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administration Chapter 2
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Introduction to Artificial Intelligence
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administration Chapter 2
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction to Artificial Intelligence
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Design an Analysis of Algorithms II-SECS-1021-03
AI in Product Development-omnex systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
Understanding Forklifts - TECH EHS Solution
Which alternative to Crystal Reports is best for small or large businesses.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
top salesforce developer skills in 2025.pdf

Learn java script

  • 2. Comments /* The code below will change the heading with id = "myH" and the paragraph with id = "myP" in my web page: */ var x = 5; // Declare x, give it the value of 5 var y = x + 2; // Declare y, give it the value of x + 2
  • 3. Variables var x = 5; var y = 6; var z = x + y;
  • 4. Data Types var x; // Now x is undefined var x; // Now x is defined var x = 5; // Now x is a Number var x = "John"; // Now x is a String var carName = "Volvo XC60"; // Using double quotes var carName = 'Volvo XC60'; // Using single quotes var x1 = 34.00; // Written with decimals var x2 = 34; // Written without decimals
  • 5. Data Types var x = true; // boolean type var y = false; var cars = ["Saab", "Volvo", "BMW"]; // array var person = {firstName:"John", lastName:"Doe", age:50 }; // Object var cars; // Value is undefined var person = null; // Value is null
  • 6. The typeof Operator typeof "John" // Returns string typeof 3.14 // Returns number typeof false // Returns boolean typeof [1,2,3,4] // Returns object typeof {name:'John', age:34} // Returns object
  • 7. Functions Syntax: functionName(parameter1, parameter2, parameter3) { code to be executed } var x = myFunction(4, 3); // Function is called, return value will end up in x function myFunction(a, b) { return a * b; // Function returns the product of a and b }
  • 8. Why Functions? • You can reuse code: Define the code once, and use it many times. • You can use the same code many times with different arguments, to produce different results.
  • 9. Functions Used as Variables Example Instead of: temp = toCelsius(32); text = "The temperature is " + temp + " Centigrade"; You can use: text = "The temperature is " + toCelsius(32) + " Centigrade";
  • 10. JavaScript Objects • In real life, a car is an object. Properties Methods car.name = Fiat car.model = 500 car.weight = 850kg car.color = white car.start() car.drive() car.brake() car.stop()
  • 11. JavaScript Objects • Objects are variables too. But objects can contain many values. • This code assigns many values (Fiat, 500, white) to a variable named car: • var car = {type:"Fiat", model:500, color:"white"}; • objectName.propertyName • objectName.methodName()
  • 12. JavaScript Math Object • The Math object allows you to perform mathematical tasks on numbers. • Math.random(); // returns a random number between 0 and 1 • Math.min(0, 150, 30, 20, -8); // returns -8 • Math.max(0, 150, 30, 20, -8); // returns 150 • Math.round(4.7); // returns 5 (rounds a number to the nearest integer) • Math.floor(4.7); // returns 4 (rounds a number down to the nearest integer) • Math.floor(Math.random() * 2); // returns a random number between 0 and 1
  • 13. JavaScript Arrays • var array-name = [item1, item2, ...]; • var cars = ["Saab", "Volvo", "BMW"]; • var name = cars[0]; • cars[0] = "Opel";
  • 14. Adding Array Elements • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits
  • 15. Looping Array Elements • var index; var fruits = ["Banana", "Orange", "Apple", "Mango"]; for (index = 0; index < fruits.length; index++) { text += fruits[index]; }
  • 16. JavaScript Array Methods • Converting Arrays to Strings • var fruits = ["Banana", "Orange", "Apple", "Mango"]; • fruits.valueOf(); • fruits.toString();
  • 17. JavaScript Array Methods • The join() method also joins all array elements into a string. • var fruits = ["Banana", "Orange","Apple", "Mango"]; • fruits.join(" * ");
  • 18. Popping and Pushing • The pop() method removes the last element from an array: • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); // Removes the last element ("Mango") from fruits • The push() method adds a new element to an array (at the end): • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi"); // Adds a new element ("Kiwi") to fruits
  • 19. Shifting Elements • The shift() method removes the first element of an array, and "shifts" all other elements one place down. • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); // Removes the first element "Banana" from fruits • The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements: • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon"); // Adds a new element "Lemon" to fruits
  • 20. Deleting Elements • var fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits[0]; // Changes the first element in fruits to undefined
  • 21. Sorting an Array • The sort() method sorts an array alphabetically: • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // Sorts the elements of fruits • The reverse() method reverses the elements in an array. • var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // Sorts the elements of fruits fruits.reverse(); // Reverses the order of the elements
  • 22. JavaScript Booleans • Very often, in programming, you will need a data type that can only have one of two values, like • YES / NO • ON / OFF • TRUE / FALSE • For this, JavaScript has a Boolean data type. It can only take the values true or false.
  • 23. The Boolean() Function • Boolean(10 > 9) // returns true • (10 > 9) // also returns true • 10 > 9 // also returns true Operator Description Example == equal to if (day == "Monday") > greater than if (salary > 9000) < less than if (age < 18)