0% found this document useful (0 votes)
4 views

Java Script 1

Uploaded by

mraffay886
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Script 1

Uploaded by

mraffay886
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA SCRIPT

SAHRISH NAZ
WHAT IS JAVA SCRIPT
 Brendan Eich developed JavaScript in 1995 at Netscape Communications:

 JavaScript is the world's most popular programming language.

 JavaScript is the programming language of the Web.

 JavaScript is easy to learn.

 Gives control over browser / system / use to execute loops etc


HELLO WORLD
 We will use these three ways to print 'Hello, World!'.

• console.log()
• alert()
• document.write()

NOTE:
 ALL CODES CAN BE EXUCUTED WITH SINGLE OR DOUBLE INVERTED
COMMAS
• CASE SENSTIVE LANGUAGE
SUM 2 NUMBERS
 const num1 = 5;
 const num2 = 3;

 // add two numbers


 const sum = num1 + num2;

 // display the sum


 console.log('The sum of ' + num1 + ' and ' + num2 + ' is: ' + sum);
CONSTANT & VERIABLES
 In JavaScript, we use the var or let keywords to declare variables. For
example,
 var age;
 let name;

• Variable names must start with a letter, an underscore _, or the dollar


sign $. For example,

let message = "hello"; // declare variable
 let _message = "hello"; num
let num;
 let $message = "hello";
// assign 5 to variable score // assign 5 to num
let score = 5; num = 5;
console.log(score); // declare variable num1 and assign 5 to it
// change the value of score to let num1 = 5;
3 // declare variable num2 and assign 6 to it
score = 3; let num2 = 6;
console.log(score); // 3
ADD TWO NUMBERS ENTERED BY THE USER
 // store input numbers
 const num1 = parseInt(prompt('Enter the first number '));
 const num2 = parseInt(prompt('Enter the second number '));

 //add two numbers


 const sum = num1 + num2;

 // display the sum


 console.log(`The sum of ${num1} and ${num2} is ${sum}`);

The above program asks the user to enter two numbers. Here, prompt() is used to take
inputs from the user. parseInt() is used to convert the user input string to number.
Finally, the sum is displayed. To display the result, we have used the template literal ` `.
This allows us to include variables inside strings.
To include variables inside ``, you need to use the ${variable} format.
SAMPLE PROGRAM
 <!DOCTYPE html>

 <html>

 <body>

 <h2>JavaScript in Body</h2>

 <p id="demo"></p>

 <script>

 document.getElementById("demo").innerHTML = "My First JavaScript";

 </script>

 </body>

 </html>

You might also like