- Simple program in JavaScript
- Using the JavaScript console to run and debug code
- Basic error handling in JavaScript
- Working with strings and numbers in JavaScript
Simple program in JavaScript that prints "Hello, World!" to the console:
console.log("Hello, World!");
You can run this program by opening the JavaScript console in your browser (usually by pressing F12 or right-clicking and selecting "Inspect Element" and then selecting the "Console" tab), and pasting the above code into the console and pressing enter.
You can also use Node.js to run JavaScript code outside of a web browser.
In order to write and run JavaScript code, you will need a text editor, such as Notepad, Sublime Text, or Visual Studio Code, and a web browser or Node.js environment.
You can also use the JavaScript console to debug your code by adding the following line before the console.log
statement
debugger;
Here is an example of basic error handling in JavaScript:
try { // code that may throw an error let x = y; // y is not defined } catch (error) { console.log(error); // Output: ReferenceError: y is not defined }
And here is an example of working with strings and numbers in JavaScript:
let firstName = "John"; let lastName = "Doe"; let fullName = firstName + " " + lastName; console.log(fullName); // Output: "John Doe"
let a = 10; let b = 20; let sum = a + b; console.log(sum); // Output: 30