
JAVASCRIPT
JS COMMENTS
*It is most important in java script.
*It is used to explain the java script code.
Single line comments
* Single line comments start with //.
*Any text between // and the end of the line will be ignored by JavaScript.
Example
let x = 5; // Declare x, give it the value of 5
Multiple line comments
*Multi-line comments start with /* and end with */.
*Any text between /* and */ will be ignored by JavaScript.
Example
/* The code below will change the heading with id = "myAnbu" and the paragraph with id = "myAnbu" */
Variables
Example
var x = 5; var y = 6; var z = x+ y;
In this example, x, y, and z, are variables, declared with the let keyword:
Operators
assignment operator
*The assignment operator (=) assigns a value to a variable.
Example
let c = 10;
addition operator
*The addition operator (+) adds numbers:
Example
let x = 5; let y = 2; let z = x + y; document.getElementById("demo").innerHTML = z;
String operator
let text1 = "John"; let text2 = "Doe"; let text3 = text1 + " " + text2; document.getElementById("demo").innerHTML = text3; Output John Doe
Logical operator
&& AND gate
|| OR gate
! NOT gate
AND gate
*It is the strict gate, It will pass only whether the 2 statement is pass
0 && 0 = 0
0 && 1 = 0
1 && 0 =0
1 && 1 = 1
OR gate
* It will pass whether the 1 statement is pass
0 || 0 = 0
0 || 1 = 1
1 || 0 = 1
1 || 1 = 1
Examples
x=4; y=7; (x > 2 && y < 5 )
* In this line we want to see whether the two condition is right.
* First we saw x is greater than 2 and y is smaller than 5.
* In this case x is greater than 2 and y is not less than 5, So it is false.
Datatypes
Strings
(let doll = "It's alright";)
Numbers
Let x=2;
Boolean
Booleans can only have two values: true or false.
let x = 5; let y = 5; let z = 6; (x == y) // Returns true (x == z) // Returns false
Array
If you have a list of items , storing the cars in single variables.
if you want to loop through the cars and find a specific one? And what
if you had not 3 cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you can
access the values by referring to an index number.
Example
const cars = ["Tata", "Volvo", "BMW"]; console.log( cars[0]); Output Tata
Object
In real life, a car is an object.
A car has properties like weight and colour, and methods like start and stop:
All cars have the same properties, but the property values differ from car to car.
const person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" }; document.getElementById("demo").innerHTML = person.firstName + " is " + person.age + " years old."; Output John is 50 years old.
Functions
A JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is executed when "something" invokes it (calls it).
Example
function myFunction(p1, p2) { return p1 * p2; } document.getElementById("demo").innerHTML = myFunction(4, 3); Output 12
If/Else
Conditional statements are used to perform different actions based on different conditions.
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Example
Const hour=20 if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } console.log( greeting); Output Good evening
Switch
* The value of the expression is compared with the values of each case.
* If there is a match, the associated block of code is executed.
* If there is no match, the default code block is executed.
Example
const day = "Monday"; switch (day) { case "Monday": console.log("Have fun"); break; case "Tuesday": console.log("Going to job"); break; case "Wednesday": console.log("playing carrom"); break; case "Thursday": console.log("playing chess"); break; case "Friday": console.log("Watching movies"); break; default: console.log("nothing else to say"); }
For loop
loops through a block of code a number of times.
Statement 1: sets a variable before the loop starts (let i = 0).
Statement 2 : defines the condition for the loop to run (i must be less than 5).
Statement 3 : increases a value (i++) each time the code block in the loop has been executed.
Example
let symbols = ["*", "**", "***", "****", "*****"]; for (let i = 0; i< 5; i++) { console.log(symbols[i]); } Output * ** *** ****
Map
*We set the value of the key in a map
*We get the value of a key in a map
Example
const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200] ]); document.getElementById("demo").innerHTML = fruits.get("apples"); Output 500