
Basics in Javascript: Javascript is a scripting language used frequently to add interaction and animation to web pages. Most large companies like Facebook, amazon, and others use Javascript to make their web pages more functional and interactive. It is mainly used for front-end web development tasks. Javascript is a major component of HTML and CSS on a web page. Let us learn more about the Basics of Javascript.
Highlights:
- Introduction to Javascript: The content introduces Javascript as a versatile, client-side scripting language used for adding interactivity to web pages. It emphasizes its role in making web pages dynamic and interactive, highlighting its standard built-in objects and predefined methods.
- Importance of Learning Javascript: It explains why learning Javascript is essential, especially for individuals interested in web development. Javascript is portrayed as a major language for creating user-friendly and dynamic web pages, with its significance underscored by its use in major companies like Google, Facebook, and Amazon.
- Hello World in Javascript: The content provides a step-by-step guide on how to write the classic "Hello World" program in Javascript. It covers creating a .js file, linking it to an HTML file, and using Javascript to manipulate HTML elements to display "Hello World" on a web page.
- Manipulating HTML with Javascript: It discusses various methods for selecting and manipulating HTML elements using Javascript. Methods such as getElementById, getElementByClassName, getElementsByTagName, and querySelector are explained, highlighting their usage in accessing and modifying HTML elements.
- Variable Declaration and Data Types: The content explains different ways to declare variables in Javascript using var, let, and const keywords. It also covers various data types supported by Javascript, including numbers, strings, arrays, functions, booleans, objects, dates, null, and undefined.
| <script src=”script/main.js”></script> |
| const helloWorld = document.querySelector(“h1”);
helloWorld.textContent = “Hello World”; |
| var element = document.getElementById(“ID1”); |
| var element = document.getElementByClassName(“className”); |
| var element = document.querySelector(“.className1”); |
| var myVariable = “Hello, World!”; |
| if (true) { let myVariable = “ This article is provided by PW Skills”; Console.log (myVariable); //Output: This article is provided by PW Skills } console.log(myVariable); // It will throw an error as let does not allow access outside the block where variables have been declared. |
| const myVariable = “Hello, World!”; |
| Basic in Javascript: Data Types | ||
| Data type | Description | Example |
| Number | Numbers are simple numerical values that are not assigned using single or double quotation marks. | let var = 99; |
| String | The sequence of characters arranged together is called a string. It is always enclosed inside a double or single quotation mark | let str = ‘Physics Wallah”; let str2 = “PWSkills”; |
| Array | In Javascript, an array is used to store multiple values in a single variable. Elements stored inside an array can be accessed using their index. | let arr = [ 1, ‘pwskills’, ‘Ankit’, 21]; |
| Function | It is used to declare a function using Javascript. Its keyword is ‘function’. | function helloWorld(name){ console.log(“Hello, “ + name + “!”); |
| Boolean | Boolean values are used to find an answer in two forms: true and false. | let isTrue = true; If (isTrue){ console.log(“True”) else{ console.log(“False”) } |
| Object | In javascript, data can be presented in the form of key-value pairs | var person = {name: “ABC”, age: 18, rollNo: 21}; |
| Date | It consists of date and time. | var currDate = new Date(); |
| Null | It represents the absence of any value inside the variable. | var nullValue = null; |
| undefined | It is used to represent an uninitialized function without a return value. | var undefinedVar; |
| Basic of Programming in Java: Operators in Java | ||
| Arithmetic Operator in Java | ||
| Operator | Description | Example |
| +(Addition) | It is used to add two or more values. | let x = 4; let y = 2; let z = x + y; //output 6 |
| -(Subtraction) | It is used to subtract two or more values. | let x = 4; let y = 2; let z = x - y; //output 2 |
| *(Multiplication) | It is used to multiply two values on either side of the operator. | let x = 4; let y = 2; let z = x *y; //output 8 |
| /(Division) | It is used to divide the left hand operator by right-hand operator | let x = 4; let y = 2; let z = x/ y; //output 2 |
| %(Modulus) | It returns the remainder when the left-hand operator is divided with the right-hand operator. | let x = 4; let y = 2; let z = x%y; //output 0 |
| Assignment Operator in Java | ||
| = | It assigns the value to a left-side operand from a right-side operand. | let x = 10 //Assigns 10 to the variable x |
| += | It keeps on adding the right operator to the left operand and assigning the result obtained to the left operand. | let a = 22 let b = 12 a +=b; //output a = 34, b = 12 |
| -= | It subtracts the right-hand operand from the left-hand operator and assigns the value to the left operator. | let a = 22 let b = 12 a -=b; //output a = 10, b = 12 |
| *= | It is used to multiply the value in the right hand to the left-hand operand. | let a = 2 let b = 2 a *=b; //output a = 4, b = 2 |
| /= | It takes the division of the two operands and then assigns the result to the left operand. | let a = 2 let b = 2 a /=b; //output a = 1, b = 2 |
| %= | It takes the modules and assigns the remainder value at the left-hand operand. | a %= b, it is the same as a= a%b |
| ^= | It is used to perform exponential calculation and assign the calculated value to the left-hand operand. | a^=b |
| Relational Operators in Java | ||
| == | It checks whether the left-hand and right-hand operands are equal. | let a = 14 let b = 11 a == b is not true. |
| != | It checks whether the left-hand and right-hand operands are not equal. | let a = 14 let b = 11 a!= b is true. |
| > | If the value of the left operand is greater than the right-hand operand, then it returns true. | let a = 14 let b = 11 a > b is true. |
| < | If the value on the left-hand side is smaller than the right-hand side, then it returns true. | let a = 14 let b = 11 a < b is false. |
| >= | It tells the left side value is greater or equal to the right-hand side. | let a = 14 let b = 11 a >=b is true. |
| <= | It tells the left value is smaller or equal to the right-hand side. | let a = 4 let b = 6 a <=b is true. |
| Logical Operators in Java | ||
| && (and) | It will return true only if both the operands are true. | a = 4 a<9 && a<10 will return true. |
| || (OR) | It will return true if any of the operands on either side are true. | a = 11 a<10 || a<12 |
| ! (NOT) | It returns true when both the operands are false. It is also used to negate the value of a variable. | a = true !a, it will return false |
| ++ | It increments the value of a variable by 1. | a = 1 a++ Now, it will return a = 2 |
| – | It will decrement the value of a variable by 1 | a = 5 a– Now, it will return a = 4 |
| Basic in Javascript |
| if(10>2){ var result = “If block Success.”; } else{ var result = “If block fails.”; } |
| Basic in Javascript |
| let num =10; if (num>10){ console.log(“The number is greater than 10.”); else if ( num<10){ console.log(“The number is lesser than 10”); else { console.log(“The number is equal to 10”); } |
| Basic in Javascript |
| // Function declaration function greetings(name) { console.log("Hello, " + name + "!"); } // Function call greet("Ankit"); // Output: Hello, Ankit! |
| Basic in Javascript |
| <button id="myButton">Click me</button> //button <script> // Function used to handle clicks. function handleClick() { alert("Button clicked!"); } // Adding event listener for click and function to handle clicks. document.getElementById("myButton").addEventListener("click", handleClick); </script> |