Understanding the basics of JavaScript syntax, values, variables, operators, and expressions is essential for building effective and efficient JavaScript applications.
JavaScript Syntax:
JavaScript code follows a specific syntax or structure, consisting of keywords, variables, and expressions. Here’s an example of a basic JavaScript code structure:
1 2 3 4 5 6 7 8 9 10 11 |
//This is a single-line comment in JavaScript /* This is a multi-line comment in JavaScript */ //Declare a variable and assign it a value let greeting = "Hello World!"; //Output the value of the variable to the console console.log(greeting); |
JavaScript Values:
JavaScript values are data that can be assigned to variables. There are two types of JavaScript values: primitive values and object values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//Primitive Values let name = "John"; //string value let age = 25; //number value let isStudent = true; //boolean value let car = null; //null value let job = undefined; //undefined value //Object Values let person = { name: "John", age: 25, isStudent: true }; let fruits = ["apple", "banana", "orange"]; |
JavaScript Variables:
JavaScript variables are containers that store data values. There are two ways to declare a variable in JavaScript: using let
or const
.
1 2 3 4 5 6 7 |
let x = 5; //declare a variable using let const y = 10; //declare a variable using const x = 7; //reassign the value of the variable x y = 12; //trying to reassign the value of the variable y will result in an error |
JavaScript Operators:
JavaScript operators are used to perform operations on values. There are different types of operators, including arithmetic, assignment, comparison, logical, and bitwise operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//Arithmetic Operators let a = 5; let b = 3; console.log(a + b); //8 console.log(a - b); //2 console.log(a * b); //15 console.log(a / b); //1.6666666666666667 console.log(a % b); //2 //Assignment Operators let c = 5; c += 3; //same as c = c + 3; console.log(c); //8 //Comparison Operators let d = 5; let e = "5"; console.log(d == e); //true (checks for equality) console.log(d === e); //false (checks for strict equality) //Logical Operators let f = true; let g = false; console.log(f && g); //false (logical AND) console.log(f || g); //true (logical OR) console.log(!f); //false (logical NOT) |
JavaScript Expressions:
JavaScript expressions produce a value. Expressions can be formed using values, variables, operators, and function calls.
1 2 3 4 5 6 |
let num1 = 5; let num2 = 3; let sum = num1 + num2; //expression that produces a value console.log(sum); //8 |
It is important to note that JavaScript is case sensitive. This means that uppercase and lowercase letters are treated as distinct characters in JavaScript code. For example, myVariable
and myvariable
are two different variables in JavaScript. It is important to be consistent with the use of capitalization in JavaScript code to avoid errors.
JavaScript Syntax Summary
In conclusion, understanding the basics of JavaScript syntax, values, variables, operators, and expressions is crucial for building effective and efficient JavaScript applications.