Variables and Operator of JavaScript

Variables are fundamental to all programming languages. Variables are used to store data, like string of text, numbers, etc. The data or value stored in the variables can be set, updated, and retrieved whenever needed. In general, variables are symbolic names for values.

You can create a variable with the var keyword, whereas the assignment operator (=) is used to assign value to a variable, like this: var varName = value;

function sho() {

var x = 2;

console .log (x);

}

sho ();

The statement below creates (in other words: declares) a variable with the name “message”:

var massege;

massege = ” Hello-world “

alert ( massage );

To be concise, we can combine the variable declaration and assignment into a single line:

alert massage = ” hello-everyone “

alert ( massage );

Variables and Types

Like almost every dynamic language, JavaScript is a “duck-typed” language, and therefore every variable is defined using the var keyword, and can contain all types of variables.

We can define several types of variables to use in our code:

var myNumber = 5;  // a number

var myString = “Hello-every!”  // a String 

var myBoolean = “true”  // a Boolean

A few notes about variable types in JavaScript:

  • In JavaScript, the Number type can be both a floating point number and an integer.
  • Boolean variables can only be equal to either true or false.

There are two more advanced types in JavaScript. An array, and an object. We will get to them in more advanced tutorials.

var myArray = [ ];  // an array

var myObject = { };  // an Object

on the top there are two special variable but it’s null

Naming Conventions for JavaScript Variables

These are the following rules for naming a JavaScript variable:

  • A variable name must start with a letter, underscore (_), or dollar sign ($).A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters (A-z, 0-9) and underscores.
  • A variable name cannot contain spaces.
  • A variable name cannot be a JavaScript keyword or a JavaScript reserved word.

Operator of javascript

Search Results Featured snippet from the web Javascript Operators. … An operator performs some operation on single or multiple operands (data value) and produces a result. For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand. + operator adds two numeric values and produces a result which is 3 in this case.
JavaScript has the following types of operators.
  • Assignment operators
  • Comparison operators
  • Bitwise operators
  • Logical operators
  • String operators
  • Conditional (ternary) operator
  • Comma operator
  • Unary operators
  • Relational operators

Assignment operators

+ (Addition)

Adds two operands

var x = 2;

var y = 3;

result = x+y;

document.write(result);  // answer will be 5

– (Subtraction)

Subtracts two operands

var x = 5;

var y = 3;

result = x-y;

document.write(result);  // 2

* (Multiplication)

Multiply both operands

var x = 2;

var y = 3;

result = x *y;

document.write(result);  // 6

/ (Division)

Divide the numerator by the denominator

var x = 5;

var y = 10;

result = y / x;

document.write(result);  // 2

% (Modulus)

Outputs the remainder of an integer division

var x = 5;

var y = 10;

result = y % x;

document.write(result);  // 0__

++ (Increment)

Increases an integer value by one

var x = 5;

x++;

document.write(x)  // 6

— (Decrement)

Decreases an integer value by one

var x = 5;

x–;

document.write(x)  // 4

Comparison Operators

JavaScript supports the following comparison operators −
Equal (==) operator
if given that x = 4 the text blow some example

x = 4

console.log(x == 4)  // true

console.log(x == ‘4’)  // true

console.log(x == 5)  // false

console.log(x == ‘ 5 ‘)  // false

Identity / strict equality (===)
The identity operator returns true if the operands are strictly equal

x = 5

console.log(x === 5)  // true

console.log(x === ‘5’)  // false

strict inequality (!==)
The non-identity operator returns true if the operands are not equal and/or not of the same type.

x ! == y

4 ! == ‘4’  // true

4 ! == 5  // true

Relational operators

Relational operators are also known as comparison operators.

  • They are used find the relationship between two values or compare.
  • relationship between two values; on comparison they yield the result true or false.

Greater than operator (>)

The greater than operator returns true if the left operand is greater than the right operand.

var x = 4

console.log ( 4 > 3 )  // true

console.log( 4 > 5 )  // false

Greater than or equal operator (>=)

The greater than or equal operator returns true if the left operand is greater than or equal to the right operand.

var x = 3

console.log(4 >= 4)  // true

console.log(4 >= 3)  // true

Less than operator (<)

The less than operator returns true if the left operand is less than the right operand.

var x = 3;

console.log( 3 < 4 )  // true

Less than or equal operator (<=)

The less than or equal operator returns true if the left operand is less than or equal to the right operand.

var x = 2;

console.log( 2 <= 3 )  // true

console.log( 2 <= 2 )  // true

Logical Operators

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they will return a non-Boolean value.

And (&&) operators

The AND operator (&&) returns true if both expressions are true, otherwise it returns false.

var x = 2;

var y = -1;

console.log( x > 0 && y > 0 ); // false

var x = 2;

var y = 1;

console.log( x > 0 && y > 0 );  // true

The Logical NOT operator (!)

Not (!) operator to a boolean value, the ! returns true if the value is false and returns false if the value if true.

var x = 5;

var y = 2;

alert( ! x > y )  // false

OR (||) operator

The OR operator (||) returns true if one or both expressions are true, otherwise it returns false.

var x = 6;

var y = 2;

console.log( x == 6 || y==1 )  // true

console.log( x == 5 || y==1 )  // false

Leave a Reply

Your email address will not be published. Required fields are marked *