JavaScript Syntax
JavaScript is one of the most popular and primary programming languages in the web development world. You need to insert your JavaScript code in an HTML file using the <script> tag.
In this article, we'll be talking about the basic JavaScript syntax which you must know in order to be a JavaScript developer. Syntax in any programming language is a standard form of representing a programming language either by adding two values or printing a statement. Here are some basic syntax rules you must follow for writing JavaScript code:
The JavaScript syntax has two types of values:
1. Fixed (Literals) - Fixed (literal) contains numbers (10.0, 100) and strings ("Guvi", "Geeks").
2. Variable (Variables) - Variables are used to store data values. JavaScript uses the keywords var, let and const to declare variables. An equal sign is used to assign values to variables. In this example, x is defined as a variable. Then, x is assigned (given) the value 6:
For example:
let x;
x = 6;
You'll read about variables in the upcoming articles - Variables in JavaScript
For example - (to add two values)
var x = 10;
var y = 12;
var z = x+y;
22
A Simple Program
Printing a statement in any language is a very basic task for any individual to learn. Therefore, this should be learned at the initial stage of their learning. Let's also learn a simple program of Hello World using JavaScript. Use an IDE of JavaScript like VS Code to run JavaScript code, or also in a web browser's console. The output will be printed to the console. To print a statement in JavaScript, we use -
Program:
console.log("Hello World");
Hello World
You can run this code in a web browser's developer console or in a JavaScript environment like Node.js. When you run the code, the text "Hello, world!" will be printed to the console.
Conclusion
In this blog, we've discussed the JavaScript syntax which every developer needs to know before starting your career as a JavaScript developer. Syntax is a very basic concept in any programming language which represents how a code/line of code should be written.