When we write console.log()
what we put inside the parentheses will get printed, or logged, to the console.
console.log(100);
A single line comment will comment out a single line and is denoted with two forward slashes //
preceding it.
// Prints 5 to the console
console.log(5);
A multi-line comment will comment out multiple lines and is denoted with /*
to begin the comment, and */
to end the comment.
/*
This is all commented
console.log(10);
None of this is going to run!
console.log(99);
*/
You can also use this syntax to comment something out in the middle of a line of code:
console.log(/*IGNORED!*/ 5); // Still just prints 5
4
, 8
, 1516
, 23.42
.' ... '
or double quotes " ... "
, though we prefer single quotes. Some people like to think of string as a fancy word for text.true
or false
(without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.null
(without quotes).undefined
(without quotes). It also represents the absence of a value though it has a different use than null
. undefined
means that a given value does not exist.The first 6 of those types are considered primitive data types. They are the most basic data types in the language.
+
-
*
/
%
Add: +
Subtract: -
Multiply: *
Divide: /
Remainder: %
console.log('hi' + 'ya'); // Prints 'hiya'
console.log('wo' + 'ah'); // Prints 'woah'
console.log('I love to ' + 'code.')
// Prints 'I love to code.'
console.log('Hello'.length); // Prints 5
和java不同,这里的length是hello这个string的属性,java里string要调用length方法返回值
console.log()
.//
and multi-line comments between /*
and */
.23.8879
'Sample String'
+
, -
, *
, /
, and %
..
after the name of the object, for example: 'Hello'.length
.'hello'.toUpperCase()
..
, dot operator.Math
, are collections of methods and properties that JavaScript provides.var myName = 'Arya';
console.log(myName);
// Output: Arya
myName
and myname
would be different variables. It is bad practice to create two variables that have the same name using different cases.let meal = 'Enchiladas';
console.log(meal); // Output: Enchiladas
meal = 'Burrito';
console.log(meal); // Output: Burrito
let
keyword, it automatically has a value of undefined
.const myPet = 'armadillo';
console.log(`I own a pet ${myPet}.`);
// Output: I own a pet armadillo.
`
(this key is usually located on the top of your keyboard, left of the 1 key).${myPet}
. The value of myPet
is inserted into the template literal.`I own a pet ${myPet}.`
, the output we print is the string: 'I own a pet armadillo.'
const unknown1 = 'foo';
console.log(typeof unknown1); // Output: string
const unknown2 = 10;
console.log(typeof unknown2); // Output: number
const unknown3 = true;
console.log(typeof unknown3); // Output: boolean
if (false) {
console.log('The code in this block will not run.');
} else {
console.log('But the code in this block will!');
}
===
!==
let myVariable = 'I Exist!';
if (myVariable) {
console.log(myVariable)
} else {
console.log('The variable does not exist.')
}
语句中的代码块将运行,这种情况下我们认为myVarialble为真值。
So which values are falsy— or evaluate to false
when checked as a condition? The list of falsy values includes:
0
""
or ''
null
which represent when there is no value at allundefined
which represent when a declared variable lacks a valueNaN
, or Not a Number 假设您有一个网站,想用用户的用户名制作个性化的问候语。有时,用户没有账户,因此用户名变量是虚假的。下面的代码会检查用户名是否已定义,如果未定义,则分配一个默认字符串:
let username = '';
let defaultName;
if (username) {
defaultName = username;
} else {
defaultName = 'Stranger';
}
console.log(defaultName); // Prints: Stranger
我们可以简写为这样:
let username = '';
let defaultName = username || 'Stranger';
console.log(defaultName); // Prints: Stranger
Because ||
or statements check the left-hand condition first, the variable defaultName
will be assigned the actual value of username
if it is truthy, and it will be assigned the value of 'Stranger'
if username
is falsy. This concept is also referred to as short-circuit evaluation.
let isNightTime = true;
if (isNightTime) {
console.log('Turn on the lights!');
} else {
console.log('Turn off the lights!');
}
==
isNightTime ? console.log('Turn on the lights!') : console.log('Turn off the lights!');
let groceryItem = 'papaya';
switch (groceryItem) {
case 'tomato':
console.log('Tomatoes are $0.49');
break;
case 'lime':
console.log('Limes are $1.49');
break;
case 'papaya':
console.log('Papayas are $1.29');
break;
default:
console.log('Invalid item');
break;
}
// Prints 'Papayas are $1.29'
if
statement checks a condition and will execute a task if that condition evaluates to true
.if...else
statements make binary decisions and execute different code blocks based on a provided condition.else if
statements.<
, >
, <=
, >=
, ===
, and !==
can compare two values.&&
, or “and”, checks if both provided expressions are truthy.||
, or “or”, checks if either provided expression is truthy.!
, switches the truthiness and falsiness of a value.if...else
statements.else if
statements. The break
keyword stops the remaining case
s from being checked and executed in a switch
statement.