Understanding JS Syntax


Understanding JS Syntax
 
console.log()
The console.log() function is used to print a greeting message. The message is printed to the console, which can be accessed using the browser's developer tools or other console applications.
Example
let name = 'John';
 console.log(`Hello, ${name}!`);

You can also display output directly in the web page by accessing HTML elements and modifying their content. To do this, you can use the document.getElementById() function to get a reference to an HTML element by its ID, and then use the innerHTML property to set its content.

<!DOCTYPE html> 
<html>
 <head> 
<title>JavaScript Output Example</title> 
</head>
 <body> 
<h1 id="greeting"></h1>
 <script> 
let name = 'John';
 let greeting = `Hello, ${name}!`;
 document.getElementById('greeting').innerHTML = greeting;
 </script>
 </body> 
</html>

In this example, the document.getElementById() function is used to get a reference to an HTML h1 element with the ID "greeting". The innerHTML property is then used to set the content of the element to the greeting message. When the page is loaded, the greeting message is displayed in the h1 element.

prompt

To input a value in JavaScript, you can use the prompt() function. The prompt() function displays a dialog box that allows the user to enter a value.
Example
let name = prompt("What's your name?"); 
console.log(`Hello, ${name}!`);

Note that the prompt() function returns a string, so if you need to use the input value as a number, you'll need to convert it using the parseInt() or parseFloat() functions.
Example
let ageString = prompt("What's your age?"); 
let age = parseInt(ageString); 
console.log(`You are ${age} years old.`);

Variables: Variables are used to store data or values. In JavaScript, you can declare variables using the var, let, or const keywords. var is the oldest way to declare variables, while let and const were introduced in ES6 (ECMAScript 2015) and are preferred for most use cases.

Data types: JavaScript has several data types including numbers, strings, booleans, null, undefined, and objects.

let a = 1; // number 
let b = 'hello'; // string
let c = true; // boolean
let d = null; // null 
let e; // undefined
let f = { name: 'John', age: 30 }; // object

Operators: JavaScript has various operators such as arithmetic, comparison, logical, and assignment operators.

let a = 5;
let b = 2; 
console.log(a + b); // 7 
console.log(a > b); // true 
console.log(a && b); // 2 
a += 3; 
console.log(a); // 8

Functions: Functions are reusable blocks of code that can be called from anywhere in your code. You can declare functions using the function keyword or using arrow functions.

function sayHello(name) { 
console.log(`Hello, ${name}!`); 
sayHello('John'); // Hello, John! 
const sum = (a, b) => a + b; 
console.log(sum(1, 2)); // 3

Conditional statements: JavaScript has if, else if, and else statements that can be used to execute different blocks of code depending on a certain condition.

let num = 5; 
if (num > 0) 
console.log("The number is positive."); 
else if (num < 0) 
console.log("The number is negative."); 
else 
console.log("The number is zero."); 
}

DOM manipulation: JavaScript can be used to manipulate the Document Object Model (DOM) which represents the structure of an HTML document. You can use JavaScript to add, remove, or modify HTML elements and content.

let element = document.createElement("div");
element.innerHTML = "Hello, World!"; document.body.appendChild(element); // adds the new element to the body of the HTML document

Classes: JavaScript supports object-oriented programming through classes. You can define classes using the class keyword and create instances of the class using the new keyword.

Example:

class Person 
constructor(name, age) 
this.name = name; 
this.age = age; 
}

sayHello() 
console.log("Hello, my name is " + this.name); 
}

let john = new Person("John", 25); 
console.log(john.age); // prints 25 to the console 
john.sayHello(); // prints "Hello, my name is John" to the console

Post a Comment

Previous Post Next Post