Tuesday, December 25, 2018

java script

Java script

Java script is the world's most popular programming language. It is the language for HTML and the web, for servers,PCs ,laptops, smart phones and more. 


Java script is a scripting language

➣A scripting language is a light weight programming language.
➢Javascript is a programming code that can be inserted into HTML pages.
➢Javascript inserted into HTML webpages, can be executes by all modern web browsers.
➢Javascript is easy to learn.

Javascript Variables

As with algebra, Javascript variables can be used to hold values (x=5) or expressions (z=x+y). Variables can have short names like x or y or more descriptive names (age, sum, total, volume)
➢Variable name must begin with a letter.
➢Variable names can be also begin with $ (but we donot use it)
➢Variable names are case sensitive( y and Y are different variables)
For example: var x=5; var y=6; var z=x+y;

Javascript function

➤It is a block of code designed to perform a particular task.
➤This function is executed when "something" calls it.
 Ex: 
<!DOCTYPE html>
<html>
<body>
<p id="function"></p>

<script>
function myFunction(p1, p2) {
  return p1 * p2;
}
document.getElementById("function").innerHTML = myFunction(4, 3);
</script>

</body>
</html>

  •                        The output is 12

Javascript objects

➤It have name and its properties like type,model... like that.
      Syntex:
      objectName.propertyName

<!DOCTYPE html>
<html>
<body>

<p id="object"></p>

<script>

var person = {firstName:"Araphi", lastName:"Jegan", age:21, eyeColor:"blue"};

document.getElementById("object").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>

</body>
</html>
                       ➤The output is Araphi is 21 years old.

Javascript array 

➤ JavaScript arrays are used to store multiple values in a single variable.
 Syntax:
var array_name = [item1item2, ...];  

<!DOCTYPE html>
<html>
<body>

<p id="array"></p>

<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("array").innerHTML = cars;
</script>

</body>
</html>
                     ➤The output is Saab,Volvo,BMW

➢The main difference between array and object is: In aray we store mant values related to the same; but in objects we store the values with different properties.
Javascript switch➤ switch statement is used to perform different actions based on different conditions like as loops.
Syntex:
switch(expression) {
  case x:
    // code block    break;
  case y:
    // code block    break;
  default:
    // code block
}  


<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
var text;
switch (new Date().getDay()) {
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
    break;
  default:
    text = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

                       ➤The output is Looking forward to the Weekend

Here,The value  is compared with the values of each case then,If there is a match, the related block of code is executed.

Javascript for loop 

Syntax:for (statement 1; statement 2; statement 3) {
  // code block to be executed
}
 

 <!DOCTYPE html>
<html>
<body>

<p id="loop"></p>

<script>
var cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
var text = "";
var i;
for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
}
document.getElementById("loop").innerHTML = text;
</script>

</body>
</html>


                  ➤ The output is BMW Volvo Saab Ford Fiat Audi



Javascript While loop

Syntax: 

do {
  // code block to be executed}
while (condition);

<!DOCTYPE html>
<html>
<body>

<p id="while"></p>

<script>
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var i = 0;
var text = "";
for (;cars[i];) {
  text += cars[i] ;
  i++;
}
document.getElementById("while").innerHTML = text;
</script>

</body>
</html>

                 ➤The output is BMW Volvo Saab Ford

Javascript break

➤The break statement breaks the loop and continues executing the code after the loop 

<!DOCTYPE html>
<html>
<body>
<p id="break"></p>

<script>
var text = "";
var i;
for (i = 0; i < 10; i++) {
  if (i === 3) { break; }
  text += "The number is " + i + "<br>";
}
document.getElementById("break").innerHTML = text;
</script>

</body>
</html>


            ➤The output will be  :
                                           The number is 0
                                           The number is 1
                                           The number is 2

 

Javascript math 

➤Math.round: It round the decimal to near value

<!DOCTYPE html>
<html>
<body>

<p id="math"></p>

<script>
document.getElementById("math").innerHTML = Math.round(4.4);
</script>

</body>
</html>

                        ➤The output will be 4.

➤Math.sqrt: It find the number of given squareroot

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>

</body>
</html>
                        ➤ The output will be 8

➤Math.min: It find the minimum number in the given task

<!DOCTYPE html>
<html>
<body>
<p id="min"></p>

<script>
document.getElementById("min").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);
</script>

</body>
</html>

No comments:

Post a Comment

React React  is a JavaScript library for building user interfaces created by Facebook. It is maintained by Facebook and a community of...