JavaScript syntax is basically a set of rules, which governs how programs are constructed.
Just like any other programming language, variables are used to store values.
In traditional programming languages like C, C++ and Java we declare variables by their type i.e. int, double, string, boolean.
But in JavaScript, we use the keyword var to declare all variables. Later when we assign values to it, the compiler decides it type.
Consider the below example. Here we are first declaring a variable num, then assigning a value 10 to it. The compiler will now mark it as Number datatype. Similarly, we are declaring variable str and assigning a string value to it in the next line. Notice the bool variable. Here we are both declaring and assigning a value at the same time.
var num; num = 10; var str; str = “I am a string”; var bool = true;
JavaScript uses arithmetic operators like + - * / to compute values. It also have other operators to compute like increment(++) and decrement(--) operator. It also have two comparison operators(== and ===).
The assignment(=) operator is used to assigning the right-hand side expression to the left variable.
We will learn in detail about the operator in the chapter on Operators. Let’s consider the below example now to watch the above-mentioned operators in action. You can run the below code in any browser’s console.
var a = 10; if( a === 10) { var res = ++a + (10 * 6); console.log(a); //11 console.log(res); //71 }
JavaScript identifiers are used to name variable. Like in most programming languages, we have rules to name variable.
Keywords are not allowed as identifiers. The first character must be a letter, or an underscore (_), or a dollar sign ($).
Subsequent characters may be letters, digits, underscores, or dollar signs.
Variable names are case-sensitive. So, firstName and firstname are different.
So, by following the above rules we can create variable names. But if there are multiple words, which needed to be joined then JS developers and the community follows the lower Camel case notation. Example is –
firstName, lastName, fullName
Like all other programming languages, we all have comments in JavaScript. Comments are skipped by the JS compiler and not executed. Developers generally use comments to make their code more readable and understandable by other developers.
There are two types of comments in JS- Single line comment and Multi-line comments.
Single line comment starts with //. Any text between // and end of the line will be ignored by JS compiler and will not be executed.
Multi-line comments start with /* and end with */. Any text between them will be ignored by JS compiler and will not be executed.
Below is an example using both Single line comment and Multi-line comments.
/*Below code checks whether the age passed is above then 18 or not */ if(age > 18) { return “Adult”; //Will return the string “Adult” } else { Return “Not adult”; //Will return the string “Not Adult” }
I have learned many things from this article. It is beneficial for me. Thank you!
Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..
This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.
Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.
You have shared great information with me i appreciate your work!
Build various types of web applications,command-line application,etc....
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *