- We can write Javascript to a html in 3 ways
-
Inline : Writing code as an inline command i.not recomended
<button type = 'submit' onclick='alert('You clicked the button')'>Submit</button>
-
Internal : Writing code attaching "script" tags in html
<script> alert('Hello World') </script>
-
External : By attaching an external file
<script src='./fileName'></script>
- 3 usefull tools while learning JS i.e 3-best friends
document.write('Hello World')
console.log('Hello People')
alert('Welcome')
- Single line comment //
- Multi line comment /* */
- Rules for declaring variables
-
Can include letters , digits and dollar and underscore, but must start with letters or & or _
-
Must not include key words
-
Case sensitive firstname != FIRSTNAME
-
CamelCasing or underscore is recomended
-
let var const are key words to declare variables
let firstName = 'Thomas '
- Adding two or more strings to form a single string is called as string concatination
let firstName = 'Thomas'
let lastName = 'Shelby'
let fullName = firstName + lastName
console.log(fullName)
- All floating and integer value numbers are classifed as number datatype in javascript
- Add +
- Substract -
- Multiply *
- Divide /
- Modules % i.e gives remainder after division
- Increment ++ i.e to increase the value by 1
- Decrement -- i.e to decrease the value by 1
- Shorthand a = a + 1 === a += 1 && a = a - 5 === a-= 5
Math operator follows BODMAS rule so ( are priority )
Javascript converts string <=> number based on action required to perform known as Implicit type conversion
//number to string
let value1 = "John";
let value2 = 123;
let result1 = value1 + value2;
console.log(result1);
console.log("converted number to : ", typeof result1);
/*String to number
+ :Concatination occurs */
let value3 = "100";
let value4 = "8";
let result2 = value3 - value4;
console.log(result2);
console.log("converted string to : ", typeof result2);
- Javascript has 5 primitive datatypes and 3 non primitive datatypes
Primitive Datatypes
- String : ' ' , " " , ``
- Number : 1 , 1.123
- Boolean : true , false
- Undefined : variable is declared but not assigned any values
- Null : No value
Non-Primitive Datatypes
- Array : can store multiple datatypes as values in zero indexed manner
- Function : block of codes which can be called whenever necessary
- Object : holds properties, values and methods
-
If Else condition When a condition is true the code in code block of if will be executed or else code block will be executed, If we want to check some more condition else if can be used
Syntax
if(condition){ //If condition is true executed } else if(condition){ //If this condition is true will be executed }else { //If both the conditions are not true will be executed }
- Used to compare 2 values
- '<' less than
- '>' greater than
- '>=' greater than equal to
- '<=' less than equal to
- '==' equal to
- '!=' not equal to
- '===' strict equal to it checks datatype also
- '!==' strict not equal to
- example
let a = 1;
let b = 2;
if (a > b) {
console.log("a > b");
} else {
console.log("a < b");
}
let x = 1;
let y = "1"; //implicit type conversion
if (x === y) {
console.log("x == y");
} else if (x == y) {
console.log("x == y");
}