What is JavaScript and why do we use it!

JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive. Where HTML and CSS are languages that give structure and style to web pages, JavaScript gives web pages interactive elements that engage a user

JavaScript Variables

When to Use JavaScript var?

Always declare JavaScript variables with var,let, orconst.

The var keyword is used in all JavaScript code from 1995 to 2015.

The let and const keywords were added to JavaScript in 2015.

If you want your code to run in older browsers, you must use var.

console.log(z)
var x = "Ananya";
var y = " is 15";
var z = x + y;
Ananya is 15
console.log(e)
var a = 5;
var b = 6;
var c = 1;
var d = 8;
var e = a + c + d + b
20

JavaScript Math!

Math.round(4.6);
5
Math.round(4.4);
4
Math.round(1.4)
1

This rounds up to the nearest intger

Math.ceil(-4.2);
-4
Math.ceil(4.2);
5

This round down to the nearest integer

Math.floor(4.9);
4
Math.floor(-6.8);
-7

Constant Objects and Arrays

The keyword const does not define a constant value. It defines a constant reference to a value. Because of this you can NOT:

  • Reassign a constant value
  • Reassign a constant array
  • Reassign a constant object

But you CAN:

  • Change the elements of constant array
  • Change the properties of constant object
// You can create a constant array:
const fruits = ["banana", "strawberry", "apple"];

// You can change an element:
fruits[1] = "mango";

// You can add an element:
fruits.push("grapes"); 
fruits.push("kiwi");
5